Java Concurrent Programming: comparison of sleep, wait and yield

First, wait () and notify (), notifyAll () are methods of the object class, sleep () and yield () are methods of the thread class. (1). The commonly used wait methods are wait() and wait (long timeout): void wait() causes the current thread to wait before other threads call the notify() method or notifyall() method of this object. Void wait (long timeout) causes the current thread to wait before other threads call the notify() method or notifyall() method of this object, or the specified amount of time is exceeded. After wait (), the thread will release the "lock flag" it holds, so that other synchronized data in the object where the thread is located can be used by other threads. Wait () and notify () must be called in the synchronized function or synchronized block because they operate on the "lock flag" of the object. If you call in a non synchronized function or non synchronized block, although it can be compiled, an exception of illegalmonitorstateexception will occur at run time. (2). Thread. Sleep (long millis) must have a time parameter. Sleep (long) brings the current thread to a standstill, so the thread executing sleep () will not be executed within the specified time; Sleep (long) can give low priority threads the chance to execute. Of course, it can also give threads with the same priority and high priority the chance to execute; Sleep (long) does not release the lock flag. (3). Yield() has no parameters. The sleep method makes the currently running thread sleep for a period of time and enter the non running state. The length of this period of time is set by the program. The yield method makes the current thread give up the possession of the CPU, but the time given up is not settable. Yield () also does not release the lock flag. In fact, the yield () method corresponds to the following operations: first, check whether threads with the same priority are in the same runnable state. If so, give the possession of the CPU to this thread, otherwise continue to run the original thread. Therefore, the yield () method is called "concession", which gives the running opportunity to other threads with the same priority. The sleep method allows the thread with lower priority to get the chance to run, but when the yield () method is executed, the current thread is still in the runnable state, so it is impossible to give up the thread with lower priority to get the CPU possession later. In a running system, if the higher priority thread does not call the sleep method and is not blocked by I / O, the lower priority thread can only wait for all the higher priority threads to run. Yield () only returns the current thread to the executable state, so the thread executing yield () may be executed immediately after entering the executable state. Therefore, yield () can only give threads of the same priority a chance to execute.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>