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

Java mutually exclusive & quot; wait-Notification & quot; Mechanism

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

Share

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

This article mainly introduces the "waiting-notification" mechanism of Java mutual exclusion. In daily operation, I believe that many people have doubts about the "waiting-notification" mechanism of Java mutual exclusion. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "waiting-notification mechanism" of Java mutual exclusion. Next, please follow the editor to study!

Needless to say, let's look at a piece of code first.

This code means that we go to the administrative service center to apply for passports, staff A to serve us, we need to give him our ID card to deal with, right? after the payment will take effect.

Because A can only serve one person at a time, we put a lock on A. But the above code is a little out of line with our realistic logic.

This is the code, that is, if you wait in line to call a number, you go to the counter and find that you do not have your ID card, then under normal circumstances, you have to go home to get your ID card. Staff A will not let you occupy a seat waiting for you, but will call the next number. How inefficient is it if you don't call the next number and wait for you to get your ID card when you get home? It's the same in our code!

So how to improve efficiency? This introduces the "wait-notify" mechanism!

Let's modify the class An above.

After this transformation, it is in line with our normal situation! But you went to the counter and found that you did not have an ID card, the staff told you sorry you go back to get it (wait)! Is to ask you to go and wait. Next!

In java, you are a thread to apply for a passport, but you don't meet the conditions, so you just wait (unlock) and don't block everyone! In this way, the efficiency has been greatly improved! Then after waiting for a person to handle it, we will notify those waiting threads to see if you have your ID cards with you now. If you bring it, come back in line!

Let's take a closer look at this process.

That is, the way we are locked will be uniformly wrapped! All the entrances and exits are controlled by synchroinzed.

Waiting for alignment is to store those waiting threads who want to enter this critical section. The condition variable corresponds to the IdCard of our above code. Calling wait () means that our condition is not satisfied, so let the current thread enter the condition variable to wait for alignment!

Then call notifyAll () surface now the condition is satisfied! Then those conditions treat all the threads in the column out to wait in the queue!

Here's why you use notifyAll () instead of notify (), because notify () randomly wakes up a condition variable to wait for threads in the queue to get them out and continue queuing! It is possible that some threads will never be notified! Isn't that over? Therefore, notifyAll () is recommended.

There's one more thing we used.

Why add a while loop to judge the condition again?

Because when you notifyAll (), your conditions are met! But you went out to wait in line again, so maybe the condition of waiting in line is not satisfied again. So you need to add a while loop!

And if you use Lock, it actually means the same thing!

So the introduction of the "wait-notify" mechanism improves the efficiency of our code execution!

At this point, the study of the "wait-notify" mechanism of Java mutual exclusion is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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