博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JDK5.0新特性系列---11.5.4线程 同步装置之Exchanger
阅读量:5116 次
发布时间:2019-06-13

本文共 2399 字,大约阅读时间需要 7 分钟。

/**

 * Exchanger让两个线程互换信息

 * 实例模拟服务生和顾客,服务生往空杯子中倒水,顾客从装满水的杯子中喝水,然后互换杯子,服务生接着倒水,顾客接着喝水.

 */

/**

 * 使用Exchanger的关键技术点如下:

 * 1.初始化Exchanger对象时,可以通过泛型指定杯子能交换的信息类型."new Exchanger<String>;"表示只能交换String类型的信息

 * 2.Exchangerexchange方法表示当前线程准备交换信息,等待其他线程与它交换信息.当有其他线程调用该Exchanger对象的exchange方法时,立即交换信息

 */

public class ExchangerTest {

       //描述一个装水的杯子

       public static class Cup{

              private boolean full = false//标识杯子是否有水

              public Cup(boolean full){

                     this.full = full;

              }

              //添水,假设需要5s

              public void addWater(){

                     if(!this.full){

                            try{

                                   Thread.sleep(5000);

                            }catch(InterruptedException e){

                            }

                            this.full = true;

                     }

              }

              //喝水,假设需要10s

              public void drinkWater(){

                     if(this.full){

                            try{

                                   Thread.sleep(10000);

                            }catch(InterruptedException e){

                                  

                            }

                            this.full = false;

                     }

              }

       }

       public static void testExchanger(){

              //初始化一个Exchanger,并规定可交换的信息类型是杯子

              final Exchanger<Cup> exchanger = new Exchanger<Cup>();

              //初始化一个空的杯子和装满水的杯子

              final Cup initialEmptyCup = new Cup(false);

              final Cup initialFullCup = new Cup(true);

             

              //服务生线程

              class Waiter implements Runnable{

                     public void run(){

                            Cup currentCup = initialEmptyCup;

                            try{

                                   int i = 0;

                                   while(i < 2){

                                          System.out.println("服务生开始往杯子里倒水: " + System.currentTimeMillis());

                                          //往空的杯子里倒水

                                          currentCup.addWater();

                                          System.out.println("服务生添水完毕: " + System.currentTimeMillis());

                                          //杯子满后和顾客的空杯子交换

                                          System.out.println("服务生等待与顾客交换杯子: " + System.currentTimeMillis());

                                          currentCup = exchanger.exchange(currentCup);

                                          System.out.println("服务生与顾客交换杯子完毕: " + System.currentTimeMillis());

                                          i++;

                                   }

                            }catch(InterruptedException ex){

                            }

                     }

              }

              //顾客线程

              class Customer implements Runnable{

                     public void run(){

                            Cup currentCup  = initialFullCup;

                            try{

                                   int i = 0;

                                   while(i < 2){

                                          System.out.println("顾客开始喝水: " + System.currentTimeMillis());

                                          //把杯子里的水喝掉

                                          currentCup.drinkWater();

                                          System.out.println("顾客喝水完毕: " + System.currentTimeMillis());

                                          //将空杯子和服务生的满杯子交换

                                          System.out.println("顾客等待与服务生交换杯子: " + System.currentTimeMillis());

                                          exchanger.exchange(currentCup);

                                          System.out.println("顾客与服务生交换杯子完毕: " + System.currentTimeMillis());

                                          i++;

                                   }

                            }catch(InterruptedException ex){

                            }

                     }

              }

              new Thread(new Waiter()).start();

              new Thread(new Customer()).start();

       }

       public static void main(String... args){

              ExchangerTest.testExchanger();

       }

}

/**

Waiter是模拟服务生的线程,首先往空杯子中添水,然后调用Exchangerexchange方法,等待和别人交换杯子.Customer是模拟了顾客的线程,首先把装满水的杯子喝光,然后调用Exchangeexchange方法,等待和别人交换杯子.当服务生和顾客都准备交换杯子时,Exchanger将服务生手中装满水的杯子和顾客手中的空杯子交换.服务生可以继续倒水,而顾客可以继续喝水.

*/

 

 

转载于:https://www.cnblogs.com/taven/archive/2011/12/17/2291477.html

你可能感兴趣的文章
如何开具和交付给客户电子发票
查看>>
heX:用HTML5和Node.JS开发桌面应用
查看>>
使用 vs 2008 宏制作自动注释工具
查看>>
js 动态生成button 并设置click事件
查看>>
esxi忘记密码重置方法
查看>>
【java】@Transactional注解与事务
查看>>
EasyPR源码剖析(4):车牌定位之Sobel算子定位
查看>>
javaweb项目搭建ehcache缓存系统
查看>>
HTML5:一个拖拽网页元素的例子
查看>>
BZOJ 2169
查看>>
js+css3实现旋转效果
查看>>
html5的web存储详解
查看>>
感觉自己应该重新读一次Javascript
查看>>
最小化安装k8s
查看>>
多线程经典模型-生产者消费
查看>>
【最短路】Walls
查看>>
Python策略模式实现源码分享
查看>>
千氪|比特币十周年大事记
查看>>
SpringMVC学习系列-解决GET请求时中文乱码的问题
查看>>
工程师追查线上问题(或运维)常用的shell命令
查看>>