Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The basic cooperation mechanism of JDK threads how to use wait and notify

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use wait and notify, the basic cooperation mechanism of JDK threads, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Preface

The communication between threads is mainly realized through wait and notify. It is very efficient to use this mechanism to realize thread communication. In comparison, students who do not know may only think of polling for thread communication. Don't poll shared variables next time and use the thread cooperation mechanism.

I. elements of thread collaboration

First of all, if you know the explicit lock ReentrantLock or explicit condition Condition, we will know that the lock queue has not only one waiting queue, but also a waiting condition queue for threads waiting to be awakened. For declarative programming synchronized keywords, the underlying principle is the same, and the corresponding queuing method for waiting condition queues is the wait/notify/notifyAll method for locking resources. But this mechanism alone can only mean that our thread can trigger other threads to continue to execute. As mentioned earlier, it is called waiting condition queue. What is the condition on earth? Generally speaking, a condition is a variable shared between threads, which is used to control the thread to wait or continue execution. To sum up, notify is generally accompanied by the change of a conditional shared variable, and wait is generally accompanied by an unsatisfied conditional shared variable. For example, the following code:

Synchronized (this) {while (! condition) {wait ();}}

At first the condition is not satisfied, the thread gives up the right to execute the CPU, enters the waiting condition queue, and then wakes up the condition sharing variable after the other thread does something else, and then the thread wakes up and continues to execute.

Second, the scene of thread cooperation

The basic cooperation mechanisms between threads can be roughly divided into the following categories:

In the producer / consumer cooperation mode, the producer thread and the consumer thread share a queue variable. In order to control the upper limit of the queue length, the producer thread is restricted to wait when the queue is full, when information is added to the queue, the queue is not empty, the consumer queue is awakened, when the queue is empty, the consumer thread is limited to wait, and when the information is fetched, the queue is not full to wake up the producer queue.

At the same time, all child threads wait for a condition according to a shared variable, which means that the main thread changes the shared variable and removes all threads in the waiting queue, such as in the simulation program. multiple threads are required to start at the same time.

Waiting for the end, the underlying principle of the thread.join () method is while (child thread not ending) {wait (0)}, waiting for the child thread to finish. A better way is to control the waiting of the main thread through the CountDownLatch class of Java. For each thread counter that ends, the waiting condition of the main thread is 0, and the thread counter can also be used to start at the same time. You only need to let the child thread share the thread counter variable, the waiting condition is that the thread counter is 0, and the main thread passes a thread counter to multiple child threads and runs the child thread. Just set the thread counter countDown to 0 at a time. This is mainly applied in the master-slave cooperation mode, where the main thread divides the task into several subtasks and creates a thread for each subtask. The main thread needs to wait for the completion of each subtask before continuing to execute other tasks.

As an asynchronous result, the jdk concurrent package encapsulates the asynchronous task, which is mainly used in master-slave cooperation scenarios. If you do not want to create threads manually, the management thread can use the Excutors asynchronous task execution service. The core is the get method of the Future interface, which waits for the asynchronous task to return the execution result before it is awakened and the result is returned.

The CyclicBarrier fence in the rendezvous point and concurrent package is the application of this scenario. A group of child threads wait for all other threads to go to one place at the same time before starting to execute the following logic. For example, in some parallel computing scenarios, each thread is responsible for part of the calculation, and then waits for the other threads to complete at the assembly point. After all threads arrive, exchange data and calculation results, and then proceed to the next calculation.

On the basic cooperation mechanism of JDK threads wait and notify how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report