Code explanation of traditional thread synchronous communication technology of Java concurrency

This paper mainly studies the code examples of Java Concurrent traditional thread synchronous communication technology, which are introduced as follows.

Let's start with a question:

After reading this question, it is obvious that the communication between threads needs to be used. First, analyze the idea: first, there must be two threads, and then there must be a cycle of 50 times in each thread, because each thread has to round-trip execute the task 50 times, the task of the main thread is executed 5 times, and the task of the sub thread is executed 10 times. Inter thread communication technology mainly uses wait () method and notify () method. The wait () method causes the current thread to wait and release the lock it holds. The notify () method wakes up a single thread waiting on this object monitor. Let's complete this inter thread communication problem step by step.

First, regardless of the communication between the main thread and the sub thread, write the tasks to be performed by each thread:

As mentioned above, the two threads each have 50 large loops and execute 50 tasks, the task of the sub thread is executed 10 times, and the task of the main thread is executed 5 times. In order to ensure the synchronization between the two threads, the synchronized synchronization code block is used, and the same lock is used: the bytecode object of the class. This ensures thread safety. However, this design is not very good. As I wrote in the deadlock in the previous section, we can put thread tasks into one class. This design mode is more structured, and it is easy to solve the synchronization problem by putting different thread tasks into the same class, because it is easy to use the same lock in a class. So modify the above procedure:

After such modification, the program structure is clearer and more robust. As long as the synchronized keyword is added to the two thread task methods, the lock this is used. However, there is no communication between the two threads. The result of execution is that the main thread circulates the task 50 times, and then the sub thread circulates the task 50 times. The reason is very simple because there is synchronized synchronization.

Let's continue to improve the program and let the two threads complete the communication described in the topic:

First of all, not to mention the specific program implementation, from the perspective of structure, we have realized the benefits of this design: there is no need to modify anything in the main function. The logic of inter thread synchronization and inter thread communication is all in the business class. Different threads in the main function only need to call the corresponding tasks placed in this class. It reflects the benefits of high clustering.

Let's look at the specific code. First, define a boolean variable to identify which thread should execute. When it is not executed by the sub thread, it will sleep. Naturally, the main thread will execute. After execution, the bshouldsub is modified and the sub thread is awakened. At this time, the sub thread will judge that if the while is not satisfied, it will not sleep and execute the sub thread task. Similarly, Just after the main thread has modified bshouldsub, when the main thread executes the task in the second loop, judge that while is satisfied, go to sleep and wait for the sub thread to wake up. In this way, the logic is very clear. The main thread and sub thread execute their respective tasks in turn. This rhythm circulates 50 times.

Another small note: it's OK to use if to judge here, but why use while? Because sometimes a thread will wake up falsely (just like a person sleepwalking. He is sleeping and stands up). If it uses if, it will not return to judge if after waking up falsely. Then it will naturally execute the task. Well, another thread is executing, and it will interact with another thread. But if it's while, it's different. Even if the thread wakes up, it will judge the while. But at this time, another thread is executing. Bshouldsub has not been modified, so it still goes into while and is asleep again. Therefore, it's safe and won't affect another thread! This is also the case in the official JDK document.

That's all for inter thread communication~

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
分享
二维码
< <上一篇
下一篇>>