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

Matters needing attention when using wait () method and notify () method in Java

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

Share

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

This article mainly talks about "precautions when using wait () method and notify () method in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "points for attention when using wait () method and notify () method in Java".

Both the wait () method and the notify () method have a prerequisite that they both acquire the lock of the current object. That is, if the wait () method and the notify () method do not acquire the lock when they are in use, the program will directly throw an exception.

We see that the program is out of line. The reason is that we did not add the synchronized keyword, so the thread could not acquire the lock, but directly threw the exception.

The wait () method releases the lock on the object immediately after execution, and other threads can still execute the synchronized synchronization method where the wait () method is located. The notify () method does not release the lock on the object immediately after execution, until the thread's synchronized synchronization method finishes execution.

We see that both threads are paused, which means that both threads have been executed. The reason is that, as analyzed above, the wait () method releases the lock of the object immediately after execution, so thread two can directly acquire the lock and execute as soon as the thread finishes execution, so the output is displayed asynchronously. Let's take a look at an example of the notify () method.

Let's see that threads are executed synchronously. The reason is that the notify () method will not release the lock of the object immediately after execution, so thread one has started successfully after executing the notify () method, but it cannot acquire the lock of the current object, because thread one has not released the lock, so thread two can only wait because the method is a synchronized synchronization method, so the output information is executed synchronously.

The essence of the wait () method is to add the current thread to the waiting queue, which is not the end of the thread because it can also be recovered. The essence of the notify () method is to take a thread in the waiting queue out of the waiting queue. But if there are many threads in the waiting queue, the notify () method simply picks one thread at random to exit the waiting queue.

We see that thread one has been awakened and then continues to execute the contents of the synchronized synchronization method.

Thread state diagram

At this point, I believe you have a deeper understanding of "matters needing attention when using the wait () method and notify () method in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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