标签:
import java.util.concurrent.CountDownLatch; public class CountDownLetchTest { public static void main(String[] args) { //创建一个锁存器,要开启该锁存器必须等待5个事件的发生 CountDownLatch countDownLetch = new CountDownLatch(5); //线程一是使用锁存器的线程 new Thread(()->{ try { countDownLetch.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("五个事件执行完毕,所以才输出了这句话哦.."); }).start(); new Thread(()->{ System.out.println("第一个事件执行完毕"); countDownLetch.countDown(); }).start(); new Thread(()->{ System.out.println("第二个事件执行完毕"); countDownLetch.countDown(); }).start(); new Thread(()->{ System.out.println("第三个事件执行完毕"); countDownLetch.countDown(); }).start(); new Thread(()->{ System.out.println("第四个事件执行完毕"); countDownLetch.countDown(); }).start(); new Thread(()->{ System.out.println("第五个事件执行完毕"); countDownLetch.countDown(); }).start(); } // 输出结果: // 第一个事件执行完毕 // 第二个事件执行完毕 // 第三个事件执行完毕 // 第四个事件执行完毕 // 第五个事件执行完毕 // 五个事件执行完毕,所以才输出了这句话哦.. //根据输出结果,如果没有等待的话,出现这种结果的可能性很小很小很小很小 }
import java.util.concurrent.CyclicBarrier; public class CyclicBarrierWithoutSpecificAfterwardAction { public static void main(String[] args) { //不指定全部到达等待界点后要执行的操作 CyclicBarrier cyclicBarrier = new CyclicBarrier(3); new Thread(()->{ System.out.println("I'm thread-0,I'm on the await-point.."); try { cyclicBarrier.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Ok,Every thread is here, I'm thread-0,I'll go..."); }).start(); new Thread(()->{ System.out.println("I'm thread-1,I'm on the await-point.."); try { cyclicBarrier.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Ok,Every thread is here, I'm thread-1,I'll go..."); }).start(); new Thread(()->{ System.out.println("I'm thread-2,I'm on the await-point.."); try { cyclicBarrier.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Ok,Every thread is here, I'm thread-2,I'll go..."); }).start(); // 输出结果: // I'm thread-0,I'm on the await-point.. // I'm thread-1,I'm on the await-point.. // I'm thread-2,I'm on the await-point.. // Ok,Every thread is here, I'm thread-2,I'll go... // Ok,Every thread is here, I'm thread-0,I'll go... // Ok,Every thread is here, I'm thread-1,I'll go... } }
public class CyclicBarrierWithSpecificAfterwardAction { public static void main(String[] args) { //指定全部到达等待界点后要执行的操作 CyclicBarrier cyclicBarrier = new CyclicBarrier(3,()->{ System.out.println("Ok,Every thread is here,Hope you every thread go dwon smoothly..."); }); new Thread(()->{ System.out.println("I'm thread-0,I'm on the await-point.."); try { cyclicBarrier.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Ok,Every thread is here, I'm thread-0,I'll go..."); }).start(); new Thread(()->{ System.out.println("I'm thread-1,I'm on the await-point.."); try { cyclicBarrier.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Ok,Every thread is here, I'm thread-1,I'll go..."); }).start(); new Thread(()->{ System.out.println("I'm thread-2,I'm on the await-point.."); try { cyclicBarrier.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("Ok,Every thread is here, I'm thread-2,I'll go..."); }).start(); // 输出结果: // I'm thread-0,I'm on the await-point.. // I'm thread-1,I'm on the await-point.. // I'm thread-2,I'm on the await-point.. // Ok,Every thread is here,Hope you every thread go dwon smoothly... // Ok,Every thread is here, I'm thread-2,I'll go... // Ok,Every thread is here, I'm thread-0,I'll go... // Ok,Every thread is here, I'm thread-1,I'll go... } }
import java.util.concurrent.Exchanger; public class ExchangeTest { public static void main(String[] args) { Exchanger<Integer> exchanger = new Exchanger<Integer>(); new Thread(()->{ int count = 1; while(count<5){ try { Integer receive = exchanger.exchange(count*2); System.out.println("Got from thread-1: "+receive); count++; } catch (Exception e) { e.printStackTrace(); } } }).start(); new Thread(()->{ int count = 1; while(count<5){ try { Integer receive = exchanger.exchange(count*1); System.out.println("Got form thread-0: "+receive); count++; } catch (Exception e) { e.printStackTrace(); } } }).start(); } // 运行结果: // Got from thread-1: 1 // Got form thread-0: 2 // Got from thread-1: 2 // Got form thread-0: 4 // Got form thread-0: 6 // Got from thread-1: 3 // Got from thread-1: 4 // Got form thread-0: 8 }
import java.util.concurrent.Phaser; public class PhaserTest { public static void main(String[] args) { //也可以不重写OnAdvace()方法,重写是为了在执行了特定的阶段之后返回,这里是执行到第三步返回,如果没有重写,直到注册的所有线程运行完了所有阶段也能正常结束 //传到构造器中的3是总共有3个线程注册到这个同步器上,问题来了,有时候并不是三个线程一直协同到最后,可能走着走着就只剩一个线程在起作用了,当一个线程到达之后并且不需要再同步的时候,达到之后注销即可,调用的方法为: Phaser phaser = new Phaser(3){ @Override protected boolean onAdvance(int phase,int parties){ if(phase==2||parties==0)return true; return false; } }; new Thread(()->{ //开始第一阶段 //获取当前是在第几阶段,如果已经结束则返回负数 int i = phaser.getPhase(); System.out.println("thread-0 phaser--"+i); phaser.arriveAndAwaitAdvance(); //开始第二阶段 i = phaser.getPhase(); System.out.println("thread-0 phaser---"+i); phaser.arriveAndAwaitAdvance(); //开始第三阶段 i = phaser.getPhase(); System.out.println("thread-0 phaser----"+i); phaser.arriveAndAwaitAdvance(); //开始第四阶段 i = phaser.getPhase(); System.out.println("thread-0 phaser----- "+i); }).start(); new Thread(()->{ //开始第一阶段 int i = phaser.getPhase(); System.out.println("thread-1 phaser--"+i); phaser.arriveAndAwaitAdvance(); //开始第二阶段 i = phaser.getPhase(); System.out.println("thread-1 phaser---"+i); phaser.arriveAndAwaitAdvance(); //开始第三阶段 i = phaser.getPhase(); System.out.println("thread-1 phaser----"+i); phaser.arriveAndAwaitAdvance(); //开始第四阶段 i = phaser.getPhase(); System.out.println("thread-1 phaser----- "+i); }).start(); new Thread(()->{ //开始第一阶段 int i = phaser.getPhase(); System.out.println("thread-2 phaser--"+i); phaser.arriveAndAwaitAdvance(); //开始第二阶段 i = phaser.getPhase(); System.out.println("thread-2 phaser---"+i); //注销啦,其实这里的注销仅仅是告诉Phaser,注册的线程少了一个,具体少了哪一个它并不知道 //phaser.arriveAndDeregister(); phaser.arriveAndAwaitAdvance(); //开始第三阶段 i = phaser.getPhase(); System.out.println("thread-2 phaser----"+i); phaser.arriveAndAwaitAdvance(); //开始第四阶段 i = phaser.getPhase(); System.out.println("thread-2 phaser----- "+i); }).start(); //运行结果 // thread-0 phaser--0 // thread-1 phaser--0 // thread-2 phaser--0 // thread-2 phaser---1 // thread-1 phaser---1 // thread-0 phaser---1 // thread-1 phaser----2 // thread-0 phaser----2 // thread-2 phaser----2 // thread-1 phaser----- -2147483645 // thread-0 phaser----- -2147483645 // thread-2 phaser----- -2147483645 } }
标签:
原文地址:http://blog.csdn.net/u013165504/article/details/51171399