In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the practical application of semaphore model in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The Java semaphore model works as follows: while running, a thread can actively stop and wait for a notification from a Java semaphore model; at this time, the thread enters the Waiting queue of the semaphore; wait for the notification before continuing to run.
In many languages, synchronization locks are represented by specialized objects, usually called Monitor. Similarly, in many languages, the Java semaphore model is usually represented by a special object name, such as Mutex,Semphore.
The Java semaphore model is much more complex than the synchronous lock model. In some systems, semaphores can even be synchronized across processes. Other semaphores even have a counting function that controls the number of threads running at the same time.
We don't need to consider such a complex model. All those complex models are derived from the most basic models. As long as we master the most basic semaphore model, the "wait / notify" model, the complex model will be easily solved.
Let's take the Java language as an example. The concepts of synchronous lock and Java semaphore model in Java language are very vague. There is no special object noun to denote synchronous lock and semaphore, only two keywords related to synchronous lock-volatile and synchronized.
Although this vagueness leads to confusion of concepts, it also avoids all kinds of misunderstandings caused by nouns such as Monitor, Mutex, Semphore and so on. We don't have to focus on the noun dispute, we can focus on understanding the actual working principle.
In the Java language, any Object Reference can be used as a synchronization lock. By the same token, any Object Reference can be used as a Java semaphore model.
The wait () method of the Object object is to wait for the notification, and the notify () method of the Object object is to issue the notification.
The specific calling method is
(1) wait for a notification of a Java semaphore model
Public static final Object signal = new Object ()
... F1 () {
Synchronized (singal) {/ / first we need to get this semaphore. This semaphore is also a synchronization lock.
/ / only after we have successfully acquired the semaphore and synchronization lock of signal, can we enter this code
Signal.wait (); / / here you want to discard semaphores. This thread will enter the Waiting queue of signal semaphores
/ / Poor. The hard-earned Java semaphore model was so abandoned.
/ / after the notification, transfer from the Waiting queue to the ready queue
/ / once you are in the ready queue, one step closer to the CPU core, you will have a chance to continue to execute the following code.
/ / you still need to compete for the signal synchronization lock before you can really continue to execute the following code. It's a bitter fate.
It is important to note the meaning of signal.wait () in the above code. Signal.wait () can easily lead to misunderstanding. Signal.wait () does not mean that signal starts wait, but that the current thread running this code starts wait the signal object, that is, to enter the Waiting queue of the signal object.
(2) Notification of a Java semaphore model
... F2 () {
Synchronized (singal) {/ / first of all, we also need to get this semaphore. It is also a synchronization lock.
/ / only after we have successfully acquired the semaphore and synchronization lock of signal, can we enter this code
Signal.notify (); / / here, we notify a thread in signal's on-call queue.
/ / if a thread waits for this notification, that thread will go to the ready queue
/ / but the thread continues to have the synchronization lock signal, and the thread continues to execute
/ / Hey, although this thread is kind enough to notify other threads
/ / however, this thread is not so elegant and gives up the synchronization lock.
/ / this thread continues to execute the following code
It is important to note that signal.notify () means. Signal.notify () does not inform signal of the object itself. Instead, it notifies other threads that are waiting for the signal semaphore.
These are the basic uses of wait () and notify () for Object.
In fact, wait () can also define the wait time, when the thread is in the on-call queue of some Java semaphore model, wait long enough, it will wait no longer, and it will move from the on-call queue to the ready queue.
In addition, there is a notifyAll () method that notifies all threads in the call queue. These details do not have an impact on the overall situation.
This is the end of the content of "practical application of semaphore model in Java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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: 235
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.