Continuous update of Java design patterns

Keywords: Java

;

1, Producer and consumer models

About the wait and notify methods in the Object class. (producer and consumer models!)

First, wait and notify methods are not thread object methods, but any java object in java

	There are both methods, because these two methods are included in the Object class.
		The wait and notify methods are not called through thread objects,
		It's not like this: t.wait(), it's not like this: t.notify()... No.

Second, what is the function of wait() method?

		Object o = new Object();
		o.wait();

		express:
			Let the thread active on the o object enter the waiting state, waiting indefinitely,
			Until awakened.
			o.wait(); the call to the method will cause "the current thread (on the O object)
			Active thread) enters the wait state.

Third, what does the notify() method do?

		Object o = new Object();
		o.notify();

		express:
			Wake up the thread waiting on the o object.
		
		There is also a notifyAll() method:
			This method wakes up all threads waiting on the o object.

concept

  • 1. Using wait method and notify method to implement producer and consumer pattern
  • 2. What is the producer and consumer model?
  • Production thread is responsible for production and consumption site is responsible for consumption.
  • The generating thread and consuming thread should be balanced.
  • This is a special business requirement, in which the wait method and the noify method are used
  • 3. wait and notify methods are not thread object methods, but common java object methods.
  • 4. wait and notify methods are based on thread synchronization. Because multithreading needs to operate a warehouse at the same time, there are thread safety problems.
  • 5. The function of the wait method: o.wait allows the thread t that is active on the O object to enter the wait state, and releases the lock of the O object occupied by the thread T before.
  • 6. The function of the notify method: o.notify() wakes up the thread waiting on the O object. It is just a notification and does not release the lock previously occupied on the O object.
  • 7. The simulation warehouse uses the list collection, assuming that only one can exist.

Code example

Here, both notifyall and notify can be used. For example, if the consumer just wakes up the producer, even if the consumer grabs the lock again, it will judge whether there is inventory in the warehouse. If not, it will still enter the wait.

public class ThreadTest16 {
    public static void main(String[] args) {
        List list  = new ArrayList();

        //Create two thread objects
        Thread t1 = new Thread(new Producer(list));

        Thread t2 = new Thread(new Consumer(list));

        t1.setName("Producer thread");
        t2.setName("Consumer thread");
        t1.start();
        t2.start();
    }
}


//producer
class Producer implements Runnable{

    //Warehouse
    private List list;

    public Producer(List list) {
        this.list = list;
    }

    @Override
    public void run() {
        //Using the dead cycle simulation to produce all the time
        while (true){
            //Lock the warehouse list object
            synchronized (list){
                if (list.size()>0){//Greater than 0, there are 1 elements
                    try {
                        list.wait();//Enter wait, release lock
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                Object obj = new Object();
                list.add(obj);
                System.out.println(Thread.currentThread().getName() + "--->" + obj);
                //Awaken consumers to consume
                list.notifyAll();
            }


        }
    }
}

//consumer
class Consumer implements Runnable{

    private List list;

    public Consumer(List list) {
        this.list = list;
    }

    @Override
    public void run() {
        //Constant consumption
        while (true){
            synchronized (list){
                if (list.size()==0){
                    try {
                        //The warehouse is empty.
                        //Consumer thread waiting, release list lock
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //Consumer consumption
                Object obj = list.remove(0);
                System.out.println(Thread.currentThread().getName() + "--->" + obj);
                //Wake up producers for production
                list.notifyAll();
            }

        }
    }
}

Posted by sseeley on Thu, 04 Jun 2020 18:38:39 -0700