Home  >  Article  >  Java  >  The differences and uses between sleep(), wait(), yield()

The differences and uses between sleep(), wait(), yield()

零下一度
零下一度Original
2017-06-30 09:56:482995browse

sleep, the thread sleeps, allowing the straight thread to pause for a period of time and enter the timing waiting state.

Method: static void sleep(long millis) After calling sleep, the current thread gives up the CPU. Within the specified time period, the thread where sleep is located will not get the opportunity to execute. Threads in this state will not release synchronization locks/synchronization listeners.

    This method is more used to simulate network delay, making the error effect of multiple threads concurrently accessing the same resource more obvious.

wait, thread communication method, the java.lang.Object class provides two types of methods for operating thread communication.

Wait(): The thread object executing this method releases the synchronization lock. The JVM stores the thread in the waiting pool and waits for other threads to wake up the thread.

Notify(): The thread executing this method wakes up any thread waiting in the waiting pool, and transfers the thread to the lock pool to wait.

notifyAll(): The thread executing this method wakes up all threads waiting in the waiting pool, and transfers the threads to the lock pool to wait.

 (Note: The above method can only be called by the synchronous monitoring lock object, otherwise an error IllegalMonitorStateException.)

yield, thread politeness, means The current thread object prompts the scheduler that it is willing to give up the CPU, but the scheduler is free to ignore the prompt.

After calling this method, the thread object enters the ready state, so it is entirely possible that after a thread calls yield(), the thread scheduler schedules it out for re-execution.

 It can be clearly seen from the documentation provided by Java7 that this method is rarely used in development. This method is mainly used for debugging or testing. It may help due to multi-thread race conditions. The following error reappears .

In addition: The difference between the sleep method and the yield method:

1. Both can cause the currently running thread to give up the CPU and give other threads the opportunity to run.

 2. The sleep method will give other threads a chance to run, but does not consider the priorities of other threads. The yield method will only give threads of the same priority or a higher priority a chance to run.

 3. After calling the sleep method, the thread enters the timing waiting state. After calling the yield method, the thread enters the ready state.

Thread lifecycle diagram:

 

The above is the detailed content of The differences and uses between sleep(), wait(), yield(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn