JAVA并发编程实践 79页 闭锁
设置线程的集合点
-
import java.util.*;
-
import java.util.concurrent.*;
-
-
public class TestHarness{
-
public long timeTasks(int nThreads,final Runnable task) throws Exception
-
{
-
final CountDownLatch startGate=new CountDownLatch(1);
-
final CountDownLatch endGate=new CountDownLatch(nThreads);
-
-
for(int i=0;i<nThreads;i++)
-
{
-
Thread t=new Thread(new Runnable(){
-
public void run()
-
{
-
try{
-
startGate.await();
-
task.run();
-
}
-
catch(Exception ex)
-
{}
-
finally{
-
endGate.countDown();
-
}
-
}
-
});
-
t.start();
-
}
-
-
long start=System.nanoTime();
-
startGate.countDown();
-
endGate.await();
-
long end=System.nanoTime();
-
return end-start;
-
}
-
-
public static void main(String[] args) throws Exception
-
{
-
TestHarness t=new TestHarness();
-
t.timeTasks(2,new Runnable(){
-
public void run(){
-
System.out.println(new Date());
-
}
-
});
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1164720/
,如需转载,请注明出处,否则将追究法律责任。