Skip to content

Instantly share code, notes, and snippets.

@cassc
Created April 5, 2014 14:03
Show Gist options
  • Save cassc/9992394 to your computer and use it in GitHub Desktop.
Save cassc/9992394 to your computer and use it in GitHub Desktop.
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