package book.j2se5;
import java.util.concurrent.Exchanger;
//模拟服务生和顾客,服务生往空杯子中倒水,顾客从装满水的杯子喝水,然后换杯子
//服务生接着喝水
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(){
//初始化一个Ex