Breaking the five Java myths: Waiting thread is awakened in the order of priority

xiaoxiao2021-03-06  87

Multiple threads waiting for a plurality of threads while writing multithreaded codes. This situation occurs in multiple threads to call the WAIT method in the synchronization method or synchronization block waiting to be locked. When another thread that locks the object is called from a synchronization method or a NOTIFYAll method, these wait threads are awakened. NOTIFY calls only awake a thread, so if there are multiple threads being in the waiting state, then there will be no competition to the lock. On the other hand, NotifyAll calls all waiting threads to cause competition, but only one thread can be locked, others will be blocked. When multiple threads are waiting to be a waiting state, which thread will run when the Notify or NotifyAll method is called? Many programmers are incorrect assumption There is a predefined order indicating how threads are awakened. Some threads think that high priority threads are first awakened, and others may think that the thread waiting for the longest time is first awake. Unfortunately, the above hypothesis is wrong. In these cases, which thread is awakened is uncertain, perhaps the highest priority thread, perhaps waiting for the longest thread, but there is no guarantee. The priority of the thread cannot determine if it is awakened (in the case of using the Notify method) or in a multi-threaded environment (in the case of using the NotifyAll method). So, you should never assume the wake-up order of the thread. In addition, you should never make any assumptions for threads in the preemptive process. Thread scheduling is implementation-dependent, and the scheduling mechanism of different platforms is different. If you think your program is portable, you should not do such an unsused assumption. In addition, NotifyAll and Notify methods do not provide determination of wake-up waiting processes, depending on the JVM, and NOTIFYALL can guarantee that things can not exceed all waiting threads awaken. This situation causes problems when you want to wake up multiple threads in a particular order. There are two ways to achieve the wake-up order of the control thread: 1, use the Specific Notification Pattern 2, use the real-time JVM (RTSJ, Real-Time Specification for Java) (Translator Note: This is not Calculate a good method, which increases the dependence of specific JVM, breaking the portability) Accurate wake-up mode is developed by Tom Cargill, in detail how to control the wake-up sequence of threads when calling Notify and Notifyall. This implementation is achieved by setting a single lock to each thread or each thread that needs to be woken up. The definable notification order is reached by release of a particular lock. If you implement proper, the cost of execution of this mode is minimal. However, it is inevitable to increase the complexity of the encoding, but this complexity can be offset by your control, if you need this control, you can consider implementing this mode. RTSJ has changed the standard behavior of certain Java semantics. One of them is to ensure that the wait thread is sorted by priority. Therefore, when multiple threads are waiting state, NOTIFY or NOTIFYALL is called, then the one with the highest priority will be executed first, and other continuation is waiting. Usually, this is not a recommended approach unless real-time programming. There are several different compromise to make Java to program in real time. One of the most important principles of creating RTSJ is that timely, more important than execution speed!

转载请注明原文地址:https://www.9cbs.com/read-125022.html

New Post(0)