Java multithreading thread communication producer consumer mode and waiting wake-up mechanism code explanation

preface

In the previous examples, multiple threads are doing the same operation. For example, four threads are doing ticketsc operation on shared data. In most cases, different threads are required to do different things in the program. For example, one thread performs tickets + + operations on shared variables and the other thread performs ticketsc operations on shared variables. This is the famous producer and consumer model.

text

1、 The producer consumer model is also multithreaded

Producer and consumer patterns are also examples of multithreading. Therefore, its programming needs to follow the rules of multithreading.

First, since it is multithreaded, synchronization must be used. Last time, when the synchronized keyword modifies a function, it uses the "this" lock, so after the function in the same class is modified by synchronized, it uses the same lock. When a thread calls these functions, whether it calls the tickets + + operation function or the ticketsc function, it will first judge whether the lock is added, and then carry out specific operations after obtaining the lock.

We first use code to express the resources, producers and consumers in the program.

When the shelf is set up, run it. Of course, there will be wrong results, as shown below:

Obviously, a thread safe error has occurred. At this time, synchronization is needed to ensure mutually exclusive access to shared variables. What needs to be synchronized in the above code is the product and consume methods in the resource class, which are modified with synchronized. Because the synchronized method is modified with "this" lock, all modified methods in the same class use the same lock, so the thread can only access one of them at a time. The resource class method after locking is as follows:

Run the code again, and there is a problem again:

Although there is no thread safety error, the problem comes. Producers keep producing. Before consumers consume, they cover the front resources with the back resources, resulting in consumers consuming less than the front resources, which is easy to cause a waste of system resources. The ideal result should be that producers produce one and consumers consume one, running harmoniously. In this regard, Java introduces a "wait wake up" mechanism for multithreading.

2、 Wait for wake-up mechanism

Different from the same operation of threads, the operation between different threads needs to wait for the wake-up mechanism to ensure the execution order between threads. In producer and consumer mode, producer and consumer are two different types of threads, and there can be many threads in these two types to work together. Generally speaking, the system sets a flag flag for resources, which is used to indicate whether resources exist. All threads should judge whether resources exist before performing operations. For example, after system initialization, resources are empty. The next execution may be the producer thread or the consumer thread. If the consumer thread obtains the execution right, judge the resource first. If it is empty at this time, it will enter the blocking state, hand over the execution right and wake up other threads. If the producer thread obtains the execution right, judge the resource first. At this time, it is empty, start production immediately, hand over the execution right and wake up other threads.

Note that there are two points mentioned above. The first point is the flag, that is, the waiting mechanism. Producers should judge that the system has no resources to produce, or wait, and consumers should judge that the system has resources to consume, or wait. The second point is the wake-up mechanism. Both producers and consumers must perform a wake-up operation after production or consumption. The wake-up waiting mechanism provided by Java is provided by Java The wait() and notify() functions in the lang.Object class. The notify() function wakes up a thread that is waiting() randomly, while notifyall() wakes up all threads that are waiting(). Unfortunately, there is no function to wake up the other thread directly.

Notify() works for single producer and single consumer modes, while notifyall() works for multi producer or multi consumer modes.

Let's take a look at the code example of two producer and two consumer threads processing a shared variable:

The output result of the above code is as follows, which is the ideal production one and consumption one in turn.

As you can see, threads 0 and 1 are producer threads, and only one of them produces at a time. Threads 2 and 3 are consumer threads. Similarly, there is only one consumer thread at a time. Note that there are two problems in the above code that need to be noted. The first is to use if or while to judge the flag, and the second is to use notify or notifyAll function. Generally speaking, while judgment will be judged again after the thread wakes up. If there is only one producer and consumer thread, you can use if. If there are multiple producers or consumers, you must use while judgment, otherwise deadlock will occur. So, you end up with a combination of while and notifyAll ().

summary

Multithreaded programming often means that multiple threads perform different tasks. Different tasks need not only "synchronization", but also "waiting for wake-up mechanism". The combination of the two can realize multi-threaded programming, in which the producer consumer model is a classic example.

However, using synchronized to modify the synchronization function and using the wait and notify method in the object class to wait for wake-up has disadvantages. The problem is efficiency. The notifyAll method wakes up all threads that are waiting, including threads of this type. If threads of this type are awakened, they have to judge again and enter the wait, which leads to a great efficiency problem. Ideally, the producer thread wakes up the consumer thread, and the consumer thread wakes up the producer thread. For this purpose, jdk1 5 introduces Java util. concurrent. Locks package, and provides lock and condition interfaces and implementation classes.

The above is all about the detailed explanation of the code of Java multithreaded thread communication producer consumer mode and waiting wake-up mechanism. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java programming multi-threaded deadlock and simple implementation code of inter thread communication, Java multi-threaded programming small example simulation parking lot system, etc. if there are deficiencies, please leave a message to point out. Thank you for your support!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>