JDK5.0新特性系列---11.5.4线程 同步装置之Exchanger

/**

 * Exchanger让两个线程互换信息

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

 */

/**

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

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

 * 2.Exchanger的exchange方法表示当前线程准备交换信息,等待其他线程与它交换信息.当有其他线程调用该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是模拟服务生的线程,首先往空杯子中添水,然后调用Exchanger的exchange方法,等待和别人交换杯子.Customer是模拟了顾客的线程,首先把装满水的杯子喝光,然后调用Exchange的exchange方法,等待和别人交换杯子.当服务生和顾客都准备交换杯子时,由Exchanger将服务生手中装满水的杯子和顾客手中的空杯子交换.服务生可以继续倒水,而顾客可以继续喝水.

*/

 

 

时间: 2024-10-31 02:44:35

JDK5.0新特性系列---11.5.4线程 同步装置之Exchanger的相关文章

JDK5.0新特性系列---11.5.1线程 同步装置之Semaphore

    import java.util.ArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Semaphore; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;   /**

JDK5.0新特性系列---11.5.2线程 同步装置之CountDownLatch

    import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors; /** * CountDownLatch维护一个计数器,等待这个CountDownLatch的线程必须等到计数器为0时才可以继续. * 以下实例模拟服务器的启动,假设启动一个服务器需要初始化3个组件,当3个组件初始化完毕后,服务器才算成功启

JDK5.0新特性系列---11.6线程 BlockingQueue

  importjava.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;   /**     本例介绍一个特殊的队列:BlockingQueue,如果BlockQueue是空的,从BlockingQueue取东西的操作将会

JDK5.0新特性系列---11.3线程 锁Lock

  import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.Reent

JDK5.0新特性系列---11.1线程 Callable和Future

  import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;   /**     从本节开始,主要介绍J2SE5.0与线程相关的新特性,新的线程类主要集中在java.util.concurrent 包中,本节实例将介绍如何使用java.uti

JDK5.0新特性系列---11.2线程 任务执行架构

  import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecu

JDK5.0新特性系列---11.4线程 Condition

  import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;   /**  *有时候线程取得lock后需要在一定条件下才能

JDK5.0新特性系列---目录

  JDK5.0新特性系列---目录   JDK5.0新特性系列---1.自动装箱和拆箱   JDK5.0新特性系列---2.新的for循环 JDK5.0新特性系列---3.枚举类型 JDK5.0新特性系列---4.静态导入 JDK5.0新特性系列---5.可变长参数Varargs JDK5.0新特性系列---6.格式化输出 JDK5.0新特性系列---7.使用ProcessBuilder执行本地命令 JDK5.0新特性系列---8.泛型编程 JDK5.0新特性系列---9.注释功能Annota

JDK5.0新特性系列---10.监控与管理虚拟机

  import java.lang.management.ClassLoadingMXBean; import java.lang.management.CompilationMXBean; import java.lang.management.GarbageCollectorMXBean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; import java.