Producer and consumer issues

Common interview questions: singleton patterns, sorting algorithms, producers and consumers, deadlocks

Synchronized wait notify

juc –>lock

Synchronized edition of producer and consumer issues

package com.chao.pc;

/** * Communication issues between threads: producer and consumer issues! The same variable num = 0 * A num+1 * B num-1 */
public class A {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"B").start(); }}// Determine wait, business, notification
class Data{// Digital resource class
    private int number = 0;

    / / + 1
    public synchronized void increment(a) throws InterruptedException {
        if(number! =0) {/ / wait for
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"= >"+number);
        // Notify other threads that I +1 is done
        this.notifyAll();
    }
    / / 1
    public synchronized void decrement(a) throws InterruptedException {
        if(number==0) {/ / wait for
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+"= >"+number);
        // Notify other threads that I am done with -1
        this.notifyAll(); }}Copy the code

There is A problem, A, B, C, D 4 threads! False awaken

If is changed to while

package com.chao.pc;

/** * Communication issues between threads: producer and consumer issues! The same variable num = 0 * A num+1 * B num-1 */
public class A {
    public static void main(String[] args) {
        Data data = new Data();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"C").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"D").start(); }}// Determine wait, business, notification
class Data{// Digital resource class
    private int number = 0;

    / / + 1
    public synchronized void increment(a) throws InterruptedException {
        while(number! =0) {/ / wait for
            this.wait();
        }
        number++;
        System.out.println(Thread.currentThread().getName()+"= >"+number);
        // Notify other threads that I +1 is done
        this.notifyAll();
    }
    / / 1
    public synchronized void decrement(a) throws InterruptedException {
        while(number==0) {/ / wait for
            this.wait();
        }
        number--;
        System.out.println(Thread.currentThread().getName()+"= >"+number);
        // Notify other threads that I am done with -1
        this.notifyAll(); }}Copy the code

JUC edition of producer and consumer issues

Find Condition by Lock

Code implementation:

package com.chao.pc;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class B {
    public static void main(String[] args) {
        Data2 data = new Data2();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.increment();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"C").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                try {
                    data.decrement();
                } catch(InterruptedException e) { e.printStackTrace(); }}},"D").start(); }}// Determine wait, business, notification
class Data2{// Digital resource class
    private int number = 0;

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    //condition.await(); / / wait for
    //condition.signalAll(); // Wake up all
    / / + 1
    public  void increment(a) throws InterruptedException {
        lock.lock();
        try {
            // Business code
            while(number! =0) {/ / wait for
                condition.await();
            }
            number++;
            System.out.println(Thread.currentThread().getName()+"= >"+number);
            // Notify other threads that I +1 is done
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}/ / 1
    public synchronized void decrement(a) throws InterruptedException {
        lock.lock();
        try {
            while(number==0) {/ / wait for
                condition.await();
            }
            number--;
            System.out.println(Thread.currentThread().getName()+"= >"+number);
            // Notify other threads that I am done with -1
            condition.signalAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}}Copy the code

Any new technology is definitely not just an overlay of the original technology, advantages and additions!

Condition notifies and wakes up threads precisely

package com.chao.pc;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/ * * *@authorJava [email protected] * A completes call B,B completes call C,C completes call D * */
public class C {
    public static void main(String[] args) {
        Data3 data = new Data3();
        new Thread(()->{
            for (int i = 0; i < 10; i++) { data.printA(); }},"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) { data.printB(); }},"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) { data.printC(); }},"C").start(); }}class Data3{ // Resource class Lock
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
    private int number = 1; //1A 2B 3C
    public void printA(a){
        lock.lock();
        try {
            // Business, judge -> execute -> notify
            while(number ! =1) {/ / wait for B
                condition1.await();
            }
            System.out.println(Thread.currentThread().getName()+"=>AAAAAAA");
            // Wake up the specified person, B
            number = 2;
            condition2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}public void printB(a){
        lock.lock();
        try {
            // Business, judge -> execute -> notify
            while(number ! =2){
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"=>BBBBBBB");
            // Wake up the specified person, C
            number = 3;
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}public void printC(a){
        lock.lock();
        try {
            // Business, judge -> execute -> notify
            while(number ! =3){
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"=>CCCCCCC");
            // Wake up the specified person, A
            number = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{ lock.unlock(); }}// Production line: Shopping: order -> payment -> transaction -> logistics
}
Copy the code