exchange java对象,【原】Java并发程序的一个应用Exchanger的实例
【原】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;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return 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;
}
@Override
public void run() {
//得到杯子喝水
/*try {
currentCup = (Cup)ex.exchange(currentCup);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.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 exception
e.printStackTrace();
}
}
if(currentCup.getWaterVolume() == 0){
System.out.println("喝水者:"+currentCup+",水喝光了!別加了!");
flag=false;
}
//服務員加完水后的杯子
try {
currentCup = (Cup)ex.exchange(currentCup);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Waiter implements Runnable{
Cup currentCup;
Exchanger ex;
Waiter(Exchanger ex,Cup c){
currentCup= c;
this.ex= ex;
}
@Override
public void run() {
//得到杯子加水
/*try {
currentCup = (Cup)ex.exchange(currentCup);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.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 exception
e.printStackTrace();
}
currentCup.addWater();
//得到顧客遞過來的杯子
try {
currentCup=(Cup)ex.exchange(currentCup);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.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 ec = new Exchanger();
ExecutorService es = Executors.newFixedThreadPool(2);
es.submit(new Waiter(ec,cup2));
es.submit(new Drinker(ec, cup1));
es.shutdown();
}
}
總結
以上是生活随笔為你收集整理的exchange java对象,【原】Java并发程序的一个应用Exchanger的实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql sha256函数_MySQL
- 下一篇: 抗日战争时期的陈建功和苏步青