【原】Java并发程序的一个使用Exchanger的实例
今天看了些Exchanger的资料,有个喝水的例子不错。我这里细化了以下,并得到实现。
?
思路:
?
???? 有一个Drinker和一个Waiter,有两个杯子,一个空杯子,一个杯子有3升水,Drinker一次喝1升水,要耗时1秒,Waiter一次可以倒1升水,一次耗时1秒。开始时,他们各持一个杯子,Drinker持有3升水的杯子,Waiter持有空杯子。然后开始喝水,当有一个杯子里没水了,整个程序结束。
?
方法:
?
???? Exchanger主要用于交换两个线程的同类型的共享数据,喝水这个例子很好的表现了Exchanger的作用。
?
【注:】程序中的数据可以自己调整来,整体体现了动态交换杯子的效果。
?
?
?
?
import java.util.concurrent.Exchanger;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;class Cup {int waterVolume = 0;String cupName="";public String getCupName() {return cupName;}public void setCupName(String cupName) {this.cupName = cupName;}Cup(int i ,String name){waterVolume=i;cupName=name;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn cupName+"有"+waterVolume+"升水!";}public int getWaterVolume() {return waterVolume;}public void drinkWater(){waterVolume--;}public void drinkWater( int i ){if((waterVolume-i)>=0){ waterVolume-=i;}else{System.out.println("没有这么多水可以喝!!!");return;}}public void addWater(){waterVolume++;}public void addWater(int i){waterVolume=i;}}class Drinker implements Runnable{Cup currentCup;Exchanger ex;Drinker(Exchanger ex,Cup c){currentCup= c;this.ex= ex;}@Overridepublic void run() {//得到杯子喝水/*try {currentCup = (Cup)ex.exchange(currentCup);} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}*/boolean flag = true;while(flag){if(currentCup.getWaterVolume() > 0){System.out.println("喝水者:"+currentCup);System.out.println("喝水者:从"+currentCup.getCupName()+"喝2升水,喝水用时1秒");currentCup.drinkWater(2);try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {// TODO: handle exceptione.printStackTrace();}}if(currentCup.getWaterVolume() == 0){System.out.println("喝水者:"+currentCup+",水喝光了!别加了!");flag=false;}//服务员加完水后的杯子try {currentCup = (Cup)ex.exchange(currentCup);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}class Waiter implements Runnable{Cup currentCup;Exchanger ex;Waiter(Exchanger ex,Cup c){currentCup= c;this.ex= ex;}@Overridepublic void run() {//得到杯子加水/*try {currentCup = (Cup)ex.exchange(currentCup);} catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}*/boolean flag = true;while(flag){System.out.println("服务员:"+currentCup);System.out.println("服务员:倒入"+currentCup.getCupName()+" 1升水,耗时1秒");try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {// TODO: handle exceptione.printStackTrace();}currentCup.addWater();//得到顾客递过来的杯子try {currentCup=(Cup)ex.exchange(currentCup);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}if(currentCup.getWaterVolume() == 0){System.out.println("服务员:"+currentCup+"水喝光了!!不加了!");flag=false;}}}}public class DrinkWaterDemo {public static void main(String[] args) {Cup cup1 = new Cup(3,"cup1");Cup cup2 = new Cup(0,"cup2");final Exchanger<Cup> ec = new Exchanger<Cup>();ExecutorService es = Executors.newFixedThreadPool(2);es.submit(new Waiter(ec,cup2));es.submit(new Drinker(ec, cup1));es.shutdown();}}
?