Home >Java >JavaInterview questions >Interviewer: Could you please tell me the differences between sleep(), wait(), join() and yield()
Let’s first introduce the concepts of lock pool and waiting pool.
First let’s look at the concept of lock pool
All threads that need to compete for synchronization locks will be placed in the lock pool. For example, the lock of the current object has been obtained by one of the threads, then other threads need to Waiting in this lock pool, when the previous thread releases the synchronization lock, the threads in the lock pool compete for the synchronization lock. When a thread obtains it, it will enter the ready queue to wait for CPU resource allocation.
Let’s take a look at the concept of waiting pool
When we call the wait() method, the thread will be placed in the waiting pool, and the threads waiting in the pool will not compete for synchronization locks. Only after calling notify() or notifyAll() will the threads in the waiting pool start to compete for the lock. notify() randomly selects a thread from the waiting pool and puts it into the lock pool, while notifyAll() puts all threads in the waiting pool Put it in the lock pool.
sleep is a static local method of the Thread class, and wait is a local method of Object.
The sleep method will not release the lock, but the wait will release it and add it to the waiting queue.
sleep就是把cpu的执行资格和执行权释放出去,不再运行此线程,当定时事件结束再取回cpu资源,参与cpu的调度,获取到cpu资源后就可以继续运行了,而如果sleep时该线程有锁,那么sleep不会释放这个锁,而是把锁带着进入了冻结状态,也就是说其它需要这个锁的线程根本不可能获取到这个锁。也就是说无法执行程序,如果在睡眠期间其它线程调用了这个线程的interrupt方法,那么这个线程也会抛出interruptexception异常返回,这点和wait是一样的。
The sleep method does not depend on the synchronizer synchronized, but wait needs to depend on the synchronized keyword.
sleep does not need to be awakened (exits blocking after sleeping), but wait does (it does not need to be interrupted by others at a specified time).
sleep is generally used for sleeping of the current thread, or polling and suspending operations, while wait is mostly used for communication between multiple threads.
sleep will give up CPU execution time and force context switching, but wait will not. After wait, there may still be a chance to compete for the lock again and continue execution.
yield() After execution, the thread directly enters the ready state and immediately releases the execution right of the cpu, but still retains the execution qualification of the cpu, so it is possible that the cpu will perform thread scheduling next time. This thread will obtain execution rights and continue execution.
(Learning video sharing: java video tutorial)
After join() is executed, the thread enters the blocking state, for example, calling join() of thread A in thread B, Then thread B will enter the blocking queue and instruct thread A to end or interrupt the thread.
public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(()->{ try { Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("休眠sleep线程"); }); t1.start(); t1.join(); System.out.println("线程执行完成"); }
Original link: https://blog.csdn.net/lxn1023143182/article/details/114134498
Related recommendations: java interview questions and answers
The above is the detailed content of Interviewer: Could you please tell me the differences between sleep(), wait(), join() and yield(). For more information, please follow other related articles on the PHP Chinese website!