CyclicBarrier类的简介和使用(2)
导读: }); Thread thread2 = new Thread(new Runnable(){ @Override public void run() { try{ System.out.println("线程2开始处理子任务..."); int result = 0; for(int i=4;i8;i++){ result+=task[i]; } result
丝瓜网小编提示,记得把"CyclicBarrier类的简介和使用(2)"分享给大家!
});
Thread thread2 = new Thread(new Runnable(){
@Override
public void run() {
try{
System.out.println("线程2开始处理子任务...");
int result = 0;
for(int i=4;i<8;i++){
result+=task[i];
}
result2= result;
System.out.println("线程2任务处理完毕!处理结果:"+result2);
cyclicBarrier.await();
}catch(Exception e){
e.printStackTrace();
}
}
});
thread1.start();
thread2.start();
}
}
运行结果如下