Lock与synchronized 的区别(3)
日期:2011-11-02
点击:133
来源: 未知
分享至:
this.threadNum=_threadNum;
cb=_cb;
preInit=new int[round];
for(int i=0;i<preInit.length;i++){
preInit[i]=r.nextInt(100);
}
}
abstract void sumValue();
abstract long getValue();
public void testTime(){
ExecutorService se=Executors.newCachedThreadPool();
long start=System.nanoTime();
for(int i=0;i<threadNum;i++){
se.execute(new Runnable(){
public void run() {
for(int i=0;i<round;i++){
sumValue();
}
try {
cb.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
});
se.execute(new Runnable(){
public void run() {
getValue();
try {
cb.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
});
}
try {
cb.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
long duration=System.nanoTime()-start;
out.println(id+" = "+duration);
}
}
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史