Exchange 애플리케이션 1

3293 단어
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<Cup> ec = new Exchanger<Cup>();
		ExecutorService es = Executors.newFixedThreadPool(2);
		es.submit(new Waiter(ec,cup2));
		es.submit(new Drinker(ec, cup1));
		es.shutdown();
		
	}
}

좋은 웹페이지 즐겨찾기