class Res { private String name; private int count = 1; private boolean flag; public synchronized void set(String name) { while (flag) { try { this.wa ...
class Res { private String name; private int count = 1; private boolean flag; public synchronized void set(String name) { while (flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.name = name + "--" + count++; System.out.println(Thread.currentThread().getName() + "...生產者..." + this.name); flag = true; this.notifyAll(); } public synchronized void print() { while (flag) { System.out.println(Thread.currentThread().getName() + "......消費者......" + this.name); flag = false; this.notifyAll(); } } } class Producer implements Runnable { private Res r; public Producer(Res r) { this.r = r; } @Override public void run() { while (true) { r.set("商品"); } } } class Consumer implements Runnable { private Res r; public Consumer(Res r) { this.r = r; } @Override public void run() { while (true) { r.print(); } } } public class ProducerConsumerDemo { public static void main(String[] args) { Res r = new Res(); new Thread(new Producer(r)).start(); new Thread(new Producer(r)).start(); new Thread(new Consumer(r)).start(); new Thread(new Consumer(r)).start(); } }
出現多個生產者消費者要用while重新判斷一次標記,並使用notifyAll()喚醒所有,notify可能出現只喚醒本方線程的情況,導致所有線程都等待。
缺點:每次都喚醒所有,其實只需要生產者喚醒消費者,消費者喚醒生產者就行了,JDK1.5中就實現了。
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; class Res { private String name; private int count = 1; private boolean flag; private Lock lock = new ReentrantLock(); private Condition condition__pro = lock.newCondition(); private Condition condition__con = lock.newCondition(); public void set(String name) throws InterruptedException { lock.lock(); try { while (flag) condition__pro.await(); this.name = name + "--" + count++; System.out.println(Thread.currentThread().getName() + "...生產者..." + this.name); flag = true; condition__con.signal(); } finally { lock.unlock(); } } public void print() throws InterruptedException { lock.lock(); try { while (!flag) condition__con.await(); System.out.println(Thread.currentThread().getName() + "......消費者......" + this.name); flag = false; condition__pro.signal(); }finally { lock.unlock(); } } } class Producer implements Runnable { private Res r; public Producer(Res r) { this.r = r; } @Override public void run() { while (true) { try { r.set("商品"); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { private Res r; public Consumer(Res r) { this.r = r; } @Override public void run() { while (true) { try { r.print(); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class ProducerConsumerDemo { public static void main(String[] args) { Res r = new Res(); new Thread(new Producer(r)).start(); new Thread(new Producer(r)).start(); new Thread(new Consumer(r)).start(); new Thread(new Consumer(r)).start(); } }
將synchronized替換成顯示的Lock操作,將Object中的wait,notify,notifyAll替換成了Condition對象,該對象可以通過Lock鎖獲取。
實現了本方只喚醒對方的操作。