Exchange 应用1

1import java.util.concurrent.Exchanger; 2import java.util.concurrent.ExecutorService; 3import java.util.concurrent.Executors; 4import java.util.concurrent.TimeUnit; 5 6class Cup { 7 8 int waterVolume = 0; 9 10 String cupName=""; 11 public String getCupName() { 12 return cupName; 13 } 14 15 public void setCupName(String cupName) { 16 this.cupName = cupName; 17 } 18 Cup(int i ,String name){ 19 waterVolume=i; 20 cupName=name; 21 } 22 @Override 23 public String toString() { 24 // TODO Auto-generated method stub 25 return cupName+"有"+waterVolume+"升水!"; 26 } 27 public int getWaterVolume() { 28 return waterVolume; 29 } 30 public void drinkWater(){ 31 waterVolume--; 32 } 33 public void drinkWater( int i ){ 34 if((waterVolume-i)>=0){ 35 waterVolume-=i; 36 }else{ 37 System.out.println("没有这么多水可以喝!!!"); 38 return; 39 } 40 } 41 public void addWater(){ 42 waterVolume++; 43 } 44 public void addWater(int i){ 45 waterVolume=i; 46 } 47} 48 49class Drinker implements Runnable{ 50 Cup currentCup; 51 Exchanger ex; 52 Drinker(Exchanger ex,Cup c){ 53 currentCup= c; 54 this.ex= ex; 55 } 56 @Override 57 public void run() { 58 //得到杯子喝水 59 /*try { 60 currentCup = (Cup)ex.exchange(currentCup); 61 } catch (InterruptedException e1) { 62 // TODO Auto-generated catch block 63 e1.printStackTrace(); 64 }*/ 65 boolean flag = true; 66 while(flag){ 67 if(currentCup.getWaterVolume() > 0){ 68 System.out.println("喝水者:"+currentCup); 69 System.out.println("喝水者:从"+currentCup.getCupName()+"喝2升水,喝水用时1秒"); 70 currentCup.drinkWater(2); 71 try { 72 TimeUnit.SECONDS.sleep(1); 73 } catch (InterruptedException e) { 74 // TODO: handle exception 75 e.printStackTrace(); 76 } 77 } 78 if(currentCup.getWaterVolume() == 0){ 79 System.out.println("喝水者:"+currentCup+",水喝光了!别加了!"); 80 flag=false; 81 } 82 //服务员加完水后的杯子 83 try { 84 currentCup = (Cup)ex.exchange(currentCup); 85 } catch (InterruptedException e) { 86 // TODO Auto-generated catch block 87 e.printStackTrace(); 88 } 89 } 90 91 } 92 93} 94 95class Waiter implements Runnable{ 96 Cup currentCup; 97 Exchanger ex; 98 Waiter(Exchanger ex,Cup c){ 99 currentCup= c; 100 this.ex= ex; 101 } 102 @Override 103 public void run() { 104 //得到杯子加水 105 /*try { 106 currentCup = (Cup)ex.exchange(currentCup); 107 } catch (InterruptedException e1) { 108 // TODO Auto-generated catch block 109 e1.printStackTrace(); 110 }*/ 111 boolean flag = true; 112 while(flag){ 113 System.out.println("服务员:"+currentCup); 114 System.out.println("服务员:倒入"+currentCup.getCupName()+" 1升水,耗时1秒"); 115 try { 116 TimeUnit.SECONDS.sleep(1); 117 } catch (InterruptedException e) { 118 // TODO: handle exception 119 e.printStackTrace(); 120 } 121 currentCup.addWater(); 122 //得到顾客递过来的杯子 123 try { 124 currentCup=(Cup)ex.exchange(currentCup); 125 } catch (InterruptedException e) { 126 // TODO Auto-generated catch block 127 e.printStackTrace(); 128 } 129 if(currentCup.getWaterVolume() == 0){ 130 System.out.println("服务员:"+currentCup+"水喝光了!!不加了!"); 131 flag=false; 132 } 133 } 134 135 } 136} 137 138 139public class DrinkWaterDemo { 140 public static void main(String[] args) { 141 Cup cup1 = new Cup(3,"cup1"); 142 Cup cup2 = new Cup(0,"cup2"); 143 final Exchanger<Cup> ec = new Exchanger<Cup>(); 144 ExecutorService es = Executors.newFixedThreadPool(2); 145 es.submit(new Waiter(ec,cup2)); 146 es.submit(new Drinker(ec, cup1)); 147 es.shutdown(); 148 149 } 150}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )