A friend who has been working for seven years does not even know why "wait" and "notify" are in the Synchronized code block.
Well, if you don't know what's in front of the screen, please put "don't know" in the comment area.
For this question, let's take a look at the answers of ordinary people and experts.
Ordinary people:
Forehead. . . . . . . . . . . .
Expert:
- wait and notify are used to achieve coordination between multiple threads. wait means to let the thread enter the blocking state, and notify means to wake up the blocked thread.
- Wait and notify must appear in pairs. If a thread is blocked by the wait() method, another thread must be woken up by the notify() method to wake up the blocked thread, thereby realizing communication between multiple threads.
In multi-threading, to achieve communication between multiple threads, in addition to pipeline flow, it can only be achieved by the method of sharing variables, that is, thread t1 modifies the shared variable s, and thread t2 obtains the modified shared variable s, thus Complete data communication.
However, multithreading itself has the characteristic of parallel execution, that is, at the same time, multiple threads can execute simultaneously. In this case, thread t2 must know that thread t1 has modified the shared variable s before accessing the shared variable s, otherwise it needs to wait.
At the same time, after thread t1 modifies the shared variable S, it also needs to notify the waiting thread t2.
Therefore, to achieve communication between threads under this feature, there must be a race condition to control under what conditions the thread waits and under what conditions it wakes up.
- The Synchronized synchronization keyword can achieve such a mutual exclusion condition, that is, in the scenario where multiple threads communicate through shared variables, the threads participating in the communication must compete for the lock resources of this shared variable in order to be eligible for the shared variable. The variable is modified, and the lock is released after the modification is completed, then other threads can compete for the lock of the same shared variable again to obtain the modified data, thus completing the previous communication of the thread.
- So this is why wait/notify needs to be placed in the Synchronized synchronization code block. With the Synchronized synchronization lock, it is possible to achieve mutual exclusion between multiple communication threads, and to achieve conditional waiting and conditional wake-up.
- In addition, in order to avoid the wrong use of wait/notify, jdk requires that wait/notify be written in the synchronous code block, otherwise an IllegalMonitorStateException will be thrown
- Finally, based on the characteristics of wait/notify, it is very suitable for implementing the producer-consumer model. For example, wait/notify is used to realize the waiting before the connection pool is ready and the wake-up after it is ready.
The above is my understanding of the problem of wait/notify.
Summarize
This is a typical classic interview question.
In fact, what is examined is the design principle and implementation principle of Synchronized and wait/notify.
Since wait/notify is rarely used in business development, most people can't answer it.
In fact, in theory, all programmers should understand the content of concurrency. Whether it is its application value or design concept, it is very worth learning and learning.
This issue of the interview series of ordinary people vs masters is over here. Friends who like it remember to like and favorite.
In addition, if you have any technical questions or professional development-related questions, you can send me a private message, and I will reply as soon as possible.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。