Created
April 5, 2014 14:03
-
-
Save cassc/9992394 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.another; | |
import java.util.concurrent.CountDownLatch; | |
public class TestThread extends Thread{ | |
public static void main(String[] args) { | |
CountDownLatch latch = new CountDownLatch(2); | |
new Thread(new TestRunnable(latch, "1")).start(); | |
new Thread(new TestRunnable(latch, "2")).start(); | |
new Thread(new TestRunnable("3")).start(); | |
new Thread(new TestRunnable("4")).start(); | |
} | |
} | |
class TestRunnable implements Runnable{ | |
private final CountDownLatch latch ; | |
private final String name; | |
public TestRunnable(CountDownLatch latch, String name){ | |
this.latch=latch; | |
this.name=name; | |
} | |
public TestRunnable(String name){ | |
this(null,name); | |
} | |
@Override | |
public void run() { | |
if (latch != null) { | |
latch.countDown(); | |
try { | |
latch.await(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
System.out.println(name+" thread starts at "+System.currentTimeMillis()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment