Producer-consumer model

Here is one of the implementations through Synchronize:

import java.util.Queue; import java.util.LinkedList; public class ProducerAndConsumer { private final int MAX_LEN = 10; private Queue<Integer> queue = new LinkedList<>(); class Producer extends Thread { @Override public void run() { producer(); } private void producer() {// Loop restriction: infinite while (true) {// lock object: Queue synchronized (queue) {// producer() stop (pause) while (queue.size() == MAX_LEN) {queue.notify(); System.out.println(" Current queue full "); try { queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } queue.add(1); queue.notify(); System.out.println(" producer produces a task, current queue length is: "+ queue.size()); Thread.sleep(500); thread.sleep (500); } catch (InterruptedException e) { e.printStackTrace(); } } } } } class Consumer extends Thread { @Override public void run() { consumer(); } private void consumer() { while (true) { synchronized (queue) { while (queue.size() == 0) { queue.notify(); System.out.println(" current queue empty "); try { queue.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } queue.poll(); queue.notify(); System.out.println(" consumer consumes a task, current queue length is: "+ queue.size()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public static void main(String[] args) { ProducerAndConsumer pc = new ProducerAndConsumer(); Producer p = pc.new Producer(); Consumer c = pc.new Consumer(); p.start(); c.start(); }}Copy the code

Two threads alternately print 1-100

Based on the above producer-consumer model, we rewrote it to have two threads alternately print 1-100,

Alternation is absolute, so define Boolean state and flip to false/true after each print to achieve alternation. Producer1 is not executed when state=false, and producer2 is not executed when state=true.

If we start printing at 1 and stop at 100, we define an int count=1, and each time we print count we add count+1. The loop limit is no longer infinite, but stops when count = 100 (while (count < 100) {… }).

public class printOneToHundred { private static volatile boolean state = true; private static volatile int count = 1; private static final Object lock = new Object(); class Producer1 extends Thread { @Override public void run() { producer1(); } private void producer1() { while (count < 100) { synchronized (lock) { while (! state) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); }} system.out. println(" thread 1 print: "+ count); count++; state = false; lock.notify(); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } } } } class Producer2 extends Thread { @Override public void run() { producer2(); } private void producer2() { while (count < 100) { synchronized (lock) { while (state) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); }} system.out. println(" thread 2 print: "+ count); count++; state = true; lock.notify(); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } } } } public static void main(String[] args) { printOneToHundred print = new printOneToHundred(); Producer1 p = print.new Producer1(); Producer2 c = print.new Producer2(); p.start(); c.start(); }}Copy the code

Output:

Print from Thread 1:1 print from thread 2:2 print from thread 1:3 print from thread 2:4 print from thread 1:5 print from thread 2:6 print from thread 1:7 print from thread 2:8 print from thread 2:9 print from thread 2:10 print from thread 1:11 print from thread 2:12 print from thread 1:13 print from thread 2: Print from 14 thread 1:15 thread 2:16 thread 1:17 thread 2:18 Thread 1:19 Thread 2:20 Thread 1:21 Thread 2:22 Thread 1:23 Thread 2:24 thread 1:25 thread 2:26 thread 1: Print from 27 thread 2:28 thread 1:29 thread 2:30 Thread 1:31 Thread 2:32 thread 1:33 Thread 2:34 thread 1:35 Thread 2:36 thread 1:37 thread 2:38 thread 1:39 Thread 2: Print from 40 thread 1:41 thread 2:42 thread 1:43 Thread 2:44 Thread 1:45 Thread 2:46 Thread 1:47 Thread 2:48 thread 1:49 Thread 2:50 thread 1:51 thread 2:52 thread 1: 53 Thread 2 print: 54 thread 1 print: 55 thread 2 print: 56 thread 1 print: 57 Thread 2 print: 58 thread 1 print: 59 thread 2 print: 60 thread 1 print: 61 thread 2 print: 62 thread 1 print: 63 thread 2 print: 64 thread 1 print: 65 thread 2 print: Print from 66 thread 1:67 thread 2:68 thread 1:69 thread 2:70 thread 1:71 Thread 2:72 thread 1:73 thread 2:74 thread 1:75 thread 2:76 thread 1:77 thread 2:78 thread 1: 79 thread 2:80 thread 1:81 thread 2:82 thread 1:83 thread 2:84 thread 1:85 thread 2:86 thread 1:87 thread 2:88 Thread 1:89 thread 2:90 thread 1:91 Thread 2: 92 Thread 1:93 thread 2:94 Thread 1:95 thread 2:96 thread 1:97 thread 2:98 thread 1:99 thread 2:100 Process finished with exit code 0Copy the code

reference

Xindoo.blog.csdn.net/article/det…