1.wait和notify
Consumer
public class Consumer extends Thread{ private int num; private Storage storage; public void run(){ consume(num); }
public void consume(int num){ storage.consume(num); } } |
Producer
public class Producer extends Thread{ private int num; private Storage storage;
public void run(){ produce(num); }
public void produce(int num){ storage.produce(num); } } |
Storage
public class Storage { private final int MAX_SIZE = 100; private LinkedList<Object> list = new LinkedList<Object>();
public void produce(int num){ synchronized (list) { while(list.size() + num > MAX_SIZE){ System.out.println("生产量:" + num + "\t 仓库余量:" + list.size() + "\t 大于最大量了!");
try{ list.wait(); }catch(InterruptedException e){ e.printStackTrace(); } }
for(int i=1; i<=num; ++i){ list.add(new Object()); }
System.out.println("已生产:" + num + "\t 目前总量:" + list.size()); list.notifyAll(); } }
public void consume(int num){ synchronized (list) { while(list.size() < num){ System.out.println("消费量:" + num + "\t 仓库总量:" + list.size() + "\t 库存不够了!"); try{ list.wait(); }catch(InterruptedException e){ e.printStackTrace(); } }
for(int i = 1; i <=num; ++i){ list.remove(); }
System.out.println("消费量:" + num + "\t 库存剩余:" + list.size());
list.notifyAll(); } }
public int getMAX_SIZE() { return MAX_SIZE; } } |
test
public class Test { public static void main(String[] args){ Storage storage = new Storage();
Producer p1 = new Producer(storage); Producer p2 = new Producer(storage); Producer p3 = new Producer(storage); Producer p4 = new Producer(storage); Producer p5 = new Producer(storage); Producer p6 = new Producer(storage); Producer p7 = new Producer(storage);
Consumer c1 = new Consumer(storage); Consumer c2 = new Consumer(storage); Consumer c3 = new Consumer(storage);
p1.setNum(10); p2.setNum(10); p3.setNum(10); p4.setNum(10); p5.setNum(10); p6.setNum(10); p7.setNum(80);
c1.setNum(50); c2.setNum(20); c3.setNum(30);
c1.start(); c2.start(); c3.start(); p1.start(); p2.start(); p3.start(); p4.start(); p5.start(); p6.start(); p7.start(); } } |
2.semaphore
Storage
public class Storage { private final int MAX_SIZE = 100; private LinkedList<Object> list = new LinkedList<Object>(); private Semaphore current_product ,remain_position, mutex;
//旋转火锅 public Storage() { current_product = new Semaphore(0);//菜品 remain_position = new Semaphore(MAX_SIZE);//空盘子 mutex = new Semaphore(1); }
//配菜员 public void produce(int num){ while (num >0) { try { remain_position.acquire();//拿一个盘子装菜, 假设只有一个空盘子. 我拿到了,别人没法装菜了. mutex.acquire();
list.add(new Object()); num--;
System.out.println(Thread.currentThread().getName() + "生产者生产了1个,仓库剩余:"+list.size()); mutex.release(); current_product.release(); } catch (InterruptedException e) { e.printStackTrace(); } } }
public void consume(int num){ while (num >0) { try { current_product.acquire();//有菜, 我可以吃了 mutex.acquire();
list.remove(); num--;
System.out.println(Thread.currentThread().getName()+ "消费者消费了1个,仓库剩余:"+list.size());
mutex.release(); remain_position.release();
} catch (InterruptedException e) { e.printStackTrace(); } } }
public LinkedList<Object> getList(){ return list; }
public void setList(LinkedList<Object> list) { this.list = list; }
public int getMAX_SIZE() { return MAX_SIZE; } } |
其他代码与wait和notify一致