Producers and consumers, using notify () wake
Package com.dwz.concurrency.chapter9; / ** * communication problems between the producer and consumer
after performed * wait () lock is released * / public class ProduceConsumerVersion4 { Private Final Object the LOCK = new new Object (); Private int 0 = I ; Private volatile Boolean isProduced = to false ; Private void Produce () { the synchronized (the LOCK) { IF (isProduced) { the try { LOCK.wait (); }catch (InterruptedException e) { e.printStackTrace(); } } else { i++; System.out.println("p->" + i); LOCK.notify(); isProduced = true; } } } private void consumer() { synchronized(LOCK) { if(isProduced) { System.out.println("c->" + i); LOCK.notify(); isProduced = false; } else { try { LOCK.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Test code (a producer and a consumer)
public static void main(String[] args) { ProduceConsumerVersion4 pc = new ProduceConsumerVersion4(); new Thread("P") { @Override public void run() { while(true) { pc.produce(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); new Thread("C") { @Override public void run() { while(true) { pc.consumer(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); }
The test results are thread-safe, the program ends normally
Test code (multiple producers and multiple consumers)
public static void main(String[] args) { ProduceConsumerVersion2 pc = new ProduceConsumerVersion2(); Stream.of("P1", "P2").forEach(n -> new Thread(n) { @Override public void run() { while(true) { pc.produce(); } } }.start() ); Stream.of("C1", "C2").forEach(n -> new Thread(n) { @Override public void run() { while(true) { pc.consumer(); } } }.start() ); }
Test results: The program does not end normally, is blocked
Between multiple producers and multiple consumers communicate using notify ()
Disadvantages: there will be a problem all threads wait (wait)
analysis: notify does not recognize the identity of the wait, it is possible to wake up the consumer is the consumer, leading to a thread We are blocked
Improved method using notifyAll () instead of the notify ()
Package com.dwz.concurrency.chapter9; Import java.util.stream.Stream; / ** communications between multiple producers and multiple consumers * (thread-safe version) * / public class ProduceConsumerVersion3 { Private Final Object the LOCK = new new Object (); Private int I = 0 ; Private volatile Boolean isProducted = to false ; Private void Produce () { the synchronized (the LOCK) { IF (isProducted) { the try { LOCK.wait (); return; } catch (InterruptedException e) { e.printStackTrace(); } } i++; System.out.println(Thread.currentThread().getName() + "for P->" + i); LOCK.notifyAll(); isProducted = true; } } private void consumer() { synchronized(LOCK) { if(!isProducted) { try { LOCK.wait(); return; } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "for C->" + i); LOCK.notifyAll(); isProducted = false; } } }
The test results, the program is thread-safe, normal execution