java怎么拦截某个对象
255
2022-08-23
java: CyclicBarrier 设置arrive action
package com.example.app;import java.util.Random;import java.util.concurrent.BrokenBarrierException;import java.util.concurrent.CyclicBarrier;import java.util.stream.IntStream;public class CyclicBarrierTest2 { private static CyclicBarrier cyclicBarrier=new CyclicBarrier(5, new Runnable() { @Override public void run() { System.out.println("all threads arrived......"); } }); public static void main(String[] args) { for(int round=0; round<3;round++){ IntStream.range(0,5).forEach( i -> new Thread(new Runnable() { @Override public void run() { int random= new Random().nextInt(5000); try { Thread.sleep(random); } catch (InterruptedException e) { e.printStackTrace(); } try { System.out.println(System.currentTimeMillis()+" "+Thread.currentThread().getName()+" got before barrier......"); cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } System.out.println(System.currentTimeMillis()+" "+Thread.currentThread().getName()+" got after barrier......"); } }).start()); } }}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~