import java.util.concurrent.Exchanger; /** * 案例:服務員向原有空杯中不斷倒水,消費者不斷從原有裝滿水的杯中喝水。 * 當服務員倒滿水和消費者喝完水時,兩個杯子進行交換。一直這樣周而復始。 * * @author Administrator * */ public class TestExchanger { static Exchanger<Cup> exchanger = new Exchanger<Cup>(); static Cup emptyCup = new Cup(0); static Cup fullCup = new Cup(100); public static void main(String[] args) { new Thread(new Waiter(3)).start(); new Thread(new Customer(6)).start(); } /** * 服務員 * * @author Administrator * */ static class Waiter implements Runnable { int addSpeed = 1; public Waiter(int addSpeed) { this.addSpeed = addSpeed; } @Override public void run() { while (emptyCup != null) { // 若是發現杯子中的水沒有滿,則往杯中加水。 try { if (emptyCup.isFull()) {// 當emptyCup爲裝滿水時而且fullCup爲被喝完水時,進行交換。 // 當Waiter把杯子水裝滿後,與Customer交換杯子。 // exchange(emptyCup)中參數的emptyCup是waiter裝滿水後的,把裝滿水後的emptyCup參數傳遞給Cutomer線程。 // 而且返回Cutomer線程中fullCup給Waiter的emptyCup,這樣就達到了交換效果。 emptyCup = exchanger.exchange(emptyCup); System.out.println("Waiter: " + emptyCup.capacity); } else { emptyCup.addWaterToCup(3); System.out.println("Waiter add " + addSpeed + " and current capacity is " + emptyCup.capacity); Thread.sleep((long) (Math .max(500, Math.random() * 2000))); } } catch (InterruptedException e) { e.printStackTrace(); } } } } /** * 消費者 * * @author Administrator * */ static class Customer implements Runnable { int drinkSpeed = 1; public Customer(int drinkSpeed) { this.drinkSpeed = drinkSpeed; } @Override public void run() { while (fullCup != null) { // 若是發現杯子中的水沒有滿,則往杯中加水。 try { if (fullCup.isEmpty()) { // 水滿後則交換子中的水 fullCup = exchanger.exchange(fullCup); System.out.println("Customer: " + fullCup.capacity); } else { fullCup.drinkFormCup(drinkSpeed); System.out.println("Customer drinked " + drinkSpeed + " and current capacity is " + fullCup.capacity); Thread.sleep((long) (Math .max(500, Math.random() * 2000))); } } catch (InterruptedException e) { e.printStackTrace(); } } } } static class Cup { private int capacity = 0; public Cup(int capacity) { this.capacity = capacity; } public void addWaterToCup(int i) { capacity += i; capacity = capacity > 100 ? 100 : capacity; } public void drinkFormCup(int i) { capacity -= i; capacity = capacity < 0 ? 0 : capacity; } public boolean isFull() { return capacity == 100 ? true : false; } public boolean isEmpty() { return capacity == 0 ? true : false; } } }