Write Multithreading
Code When multiple threads wait for an event. This often happens when multiple threads call the wait method in the synchronous method or synchronization block to wait for the same locked object. When the other thread that locks the object calls the Y or yyall method from the synchronization method or synchronization block, these waiting threads are awakened. Notify calls only wake up one thread. Therefore, if multiple threads are waiting, there will be no competition for locks. On the other hand, policyall calls wake up all the waiting threads and cause competition. However, only one thread can get the lock, and others will be blocked.
When multiple threads are waiting, which thread will run after the Y or yyall method is called? Many
Program The assumption that there is a predefined order indicating how the thread is awakened. Some threads that are considered to be of high priority are first awakened, while others may think that threads that have waited for the longest time are first awakened. Unfortunately, the above assumptions are incorrect. In these cases, it is uncertain which thread is awakened, maybe the thread with the highest priority, maybe the thread with the longest wait, but it is not guaranteed.
The thread priority cannot determine whether it is awakened (when the notify method is used) or the wake-up sequence in a multi-threaded environment (when the yyall method is used ). Therefore, you should never assume the wake-up sequence of threads. In addition, you should never make any assumptions about thread scheduling during the preemption process. Thread Scheduling is related to implementation (implementation-dependent). The Scheduling Mechanisms of different platforms are different. If you want your program to be portable, you should not make such unwise assumptions.
In addition, the yyall and notify methods do not provide a definite sequence of wake-up Wait processes. The specific sequence is dependent on JVM, and the tasks that yyall can ensure cannot exceed all the waiting threads. This situation causes problems when you want to wake up multiple threads in a specific order.
There are two ways to achieve the wake-up sequence of the control thread:
1. Use the exact wake-up mode (
Specific notification pattern)
2. Use the JVM (rtsj, real-time specification for Java) that implements real-time specifications, this increases the dependency on specific JVM and breaks the portability)
The precise wake-up mode is developed by Tom Cargill. It describes in detail how to control the wake-up sequence of threads that call y and notifyall. This implementation is achieved by setting a separate lock for each thread or set of threads to be awakened together. The order of notification can be defined by releasing a specific lock.
If the implementation is appropriate, the execution cost of this mode is the minimum. However, it is inevitable to increase the encoding complexity, but this complexity can be offset by your control. If you need such control, you can consider implementing this mode.
Rtsj changes the standard behavior of some java semantics. One of them is to ensure that the waiting threads are sorted by priority. Therefore, when multiple threads are in the waiting state and call policy or policyall, the one with the highest priority will be executed first, and the others will continue waiting.
Generally, this is not recommended unless it is real-time programming. There are already several different trade-offs that allow Java to program in real time. The most important principle for creating rtsj is that timeliness is more important than execution speed!