Traditional thread producer - consumer model

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/longgeqiaojie304/article/details/91353610

Traditional thread producer - consumer model

Manufacturers face questions:

We all know that ArrayList is not thread-safe, please write coding unsafe and gives the case a solution?

Lock fair / unfair lock / reentrant lock / recursive lock / spin locks talk about your understanding? Please handwriting a spin lock.

CountDownLatch, CyclicBarrier, Semaphore used it?

Blocking queue know?

Thread pool used it? ThreadPoolExecutor talk about your understanding?

Thread pool used it? Production How do you set reasonable parameters?

Deadlock coding and positioning analysis?

1, the traditional thread producer - consumer model is what?

2, the traditional producer - consumer model code verification Version 2.0

Demo One: do not judge while using a false wake-up problem

package com.wwl.juc;

 

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

 

/**

 * Shared resource thread operations

 */

class ShareData {

    private int num = 0;

    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

 

    // add operation +1

    public void increment() {

        lock.lock();

        try {

            while (num != 0) {

                // wait producer thread production

                condition.await();

            }

            // producer thread production operations

            a ++;

            System.out.println(Thread.currentThread().getName() + "\t" + num);

            // notify the producer thread wake up the consumer thread

            condition.signalAll();

        } catch (Exception e) {

            e.printStackTrace ();

        } finally {

            lock.unlock();

        }

    }

 

    // subtraction -1

    public void decrement() {

        lock.lock();

        try {

            while (num == 0) {

                // thread waits for consumer consumption

                condition.await();

            }

            // consumer thread consumption operation

            on one--;

            System.out.println(Thread.currentThread().getName() + "\t" + num);

            // wake-up thread notifies the consumer thread production

            condition.signalAll();

        } catch (Exception e) {

            e.printStackTrace ();

        } finally {

            lock.unlock();

        }

    }

}

 

/**

 * Interview questions: a variable initial value 0, the two threads operate them alternately, a plus 1, minus 1 a, 5 cycles of alternating

 * Traditional model of producers and consumers Demo

 * 1 , thread operations resource sharing class ShareData

 * 2 , thread determines (the while) whether to perform a subtraction operation

 * 3 , the thread after the operation is completed, the need to inform (condition.signalAll ()) other threads

 * 4 , to prevent false wake-up mechanism (if and while the use of what? Of course while using to judge)

 */

public class ProducerAndConsumerDemo {

    public static void main(String[] args) {

        ShareData shareData = new ShareData();

        // perform an addition operation thread t1

        new Thread(() -> {

            for (int i = 0; i < 5; i++) {

                shareData.increment();

            }

        }, "t1").start();

 

        // thread t2 subtraction operation

        new Thread(() -> {

            for (int i = 0; i < 5; i++) {

                shareData.decrement();

            }

        }, "t2").start();

    }

}

Program execution results are as follows: Producer thread and the Consumer thread alternately perform addition and subtraction operations

Demo Two: to determine if the operation will use a false wake-up problem (analog producer and consumer threads is greater than 2, I'm here with two threads and two producers consume this thread)

package com.wwl.juc;

 

import java.util.concurrent.locks.Condition;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

 

/**

 * Shared resource thread operations

 */

class ShareData {

    private int num = 0;

    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

 

    // add operation +1

    public void increment() {

        lock.lock();

        try {

            if (num != 0) {

                // wait producer thread production

                condition.await();

            }

            // producer thread production operations

            a ++;

            System.out.println(Thread.currentThread().getName() + "\t" + num);

            // notify the producer thread wake up the consumer thread

            condition.signalAll();

        } catch (Exception e) {

            e.printStackTrace ();

        } finally {

            lock.unlock();

        }

    }

 

    // -1 subtraction operation

    public void decrement() {

        lock.lock();

        try {

            if (num == 0) {

                // thread waits for consumer consumption

                condition.await();

            }

            // consumer thread consumption operation

            on one--;

            System.out.println(Thread.currentThread().getName() + "\t" + num);

            // wake-up thread notifies the consumer thread production

            condition.signalAll();

        } catch (Exception e) {

            e.printStackTrace ();

        } finally {

            lock.unlock();

        }

    }

}

 

/**

 * Interview questions: a variable initial value 0, the two threads operate them alternately, a plus 1, minus 1 a, 5 cycles of alternating

 * Traditional model of producers and consumers Demo

 * 1 , thread operations resource sharing class ShareData

 * 2 , thread determines (the while) whether to perform a subtraction operation

 * 3 , the thread after the operation is completed, the need to inform (condition.signalAll ()) other threads

 * 4 , to prevent false wake-up mechanism (if and while the use of what? Of course while using to judge)

 */

public class ProducerAndConsumerDemo {

    public static void main(String[] args) {

        ShareData shareData = new ShareData();

        // thread t1, an addition operation

        new Thread(() -> {

            for (int i = 0; i < 5; i++) {

                shareData.increment();

            }

        }, "t1").start();

 

        // thread t2 subtraction operation

        new Thread(() -> {

            for (int i = 0; i < 5; i++) {

                shareData.decrement();

            }

        }, "t2").start();

 

        // thread t3 an addition operation

        new Thread(() -> {

            for (int i = 0; i < 5; i++) {

                shareData.increment();

            }

        }, "t3").start();

 

        // thread t4 subtraction operation

        new Thread(() -> {

            for (int i = 0; i < 5; i++) {

                shareData.decrement();

            }

        }, "t4").start();

    }

}

Program execution results are as follows: Producer thread and the Consumer thread does not perform addition and subtraction operations alternately

 

 

Guess you like

Origin blog.csdn.net/longgeqiaojie304/article/details/91353610