I 've been in the exam twice, and I 've got a third wrong time. I changed my surname! Every day, the test interview turns around, and the mood is not found wrong! You can't be stupid anymore!
From: http://nneverwei.javaeye.com/blog/494280
Http://zhidao.baidu.com/question/75503928.html? Fr = ala0
1. The two methods are fromDifferent classesRespectively, Sleep comes from the Thread class, and wait comes from the object class.
Sleep is the static class method of thread. Whoever calls the sleep, even if the sleep method of B is called in thread a, actually a goes to bed, sleep should be called in B's code to make the B thread sleep.
2,Most importantYesNo sleep methodRelease lockThe wait method releases the lock so that other threads can use synchronous control blocks or methods.
Sleep does not assign system resources; wait enters the thread wait pool to wait, and transfers system resources. Other threads can occupy the CPU. Generally, wait does not apply a time limit because it is useless if the Running Resources of the wait thread are insufficient. Wait for other threads to call y/yyall to wake up all threads in the waiting pool, wait until the OS allocates system resources. Sleep (milliseconds) can automatically wake up with a specified time. If the time is not enough, interrupt () can only be called to force the interruption.
Thread. Sleep (0) is used to "trigger the operating system to immediately re-compete with the CPU ".
3,Scope of use:Wait, policy, and policyall can only be used in synchronous control methods or synchronization control blocks, while sleep can be used anywhere.
Synchronized (x ){
X. Sort y ()
// Or wait ()
}
4. Sleep is required.Capture exceptionsWhile wait, policy, and policyall do not need to capture exceptions.