Sleep is a threading class (thread) method that causes this thread to pause execution for a specified time, giving the execution opportunity to other threads, but the monitoring state remains and is automatically restored when it is finished. Calling sleep does not release the object lock. Wait is a method of the object class that calls the wait method on this object to cause this thread to discard the object lock, enter the waiting lock pool waiting for this object, and only after the Notify method (or Notifyall) is issued for this object the thread enters the object lock pool ready to get the object lock into the running state.
Sleep is the thread that is executing the active cpu,cpu to execute other threads, the CPU will go back to this thread after the time specified by sleep, and if the current thread enters a sync lock, the Sleep method does not release the lock. Even if the current thread uses the Sleep method to give out the CPU, other threads that are blocked by the sync lock cannot be executed. Wait refers to a thread that has entered a sync lock, allowing itself to temporarily let out a sync lock so that other threads waiting for the lock can get a synchronous lock and run, and only other threads call the Notify method (notify does not release the lock, Just tell the thread that called the wait method to be able to participate in the competition to get the lock, but not immediately get the lock, because the lock is still in the hands of others, others have not been released. If the code behind the Notify method is much more, it needs to be executed before releasing the lock, you can add a wait and some code after the Notfiy method, see the effect), the thread that calls the wait method will dismiss the wait state and the program can continue to run down after the lock is received again. Wait for the explanation must be in line with the example code to illustrate, only to appear that they really understand.
Package Com.huawei.interview;
Public class multithread {
/**
* @param args
*/
Public Static void Main (string[] args) {
TODO auto-generated Method stub
New Thread (new Thread1 ()). Start ();
Try {
Thread. Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
New Thread (new Thread2 ()). Start ();
}
Private Static class Thread1 implements Runnable
{
@Override
Public void Run () {
TODO auto-generated Method stub
Since the Thread1 and the following Thread2 internal run methods use the same object as the monitor, we cannot use this here because the this in Thread2 and this Thread1 is not the same object. We use Multithread.class, the byte-code object, which refers to the same object when the variable is referenced in the current virtual machine.
synchronized (multithread. class) {
System. out. println ("Enter thread1 ...");
System. out. println ("Thread1 is Waiting");
Try {
There are two ways to release a lock, the first being that the program naturally leaves the range of the monitor, that is, the code range that leaves the Synchronized keyword, and the wait method for the monitor object within the code that the Synchronized keyword governs. Here, use the Wait method to release the lock.
Multithread. class. Wait ();
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System. out. println ("Thread1 are going on ...");
System. out. println ("Thread1 is being over!");
}
}
}
Private Static class Thread2 implements Runnable
{
@Override
Public void Run () {
TODO auto-generated Method stub
synchronized (multithread. class) {
System. out. println ("Enter thread2 ...");
System. out. println ("Thread2 notify other thread can release wait status.");
Because the Notify method does not release the lock, even though Thread2 calls the sleep method below for 10 milliseconds, Thread1 still does not execute because THREAD2 does not release the lock, so Thread1 cannot get the lock.
Multithread. class. Notify ();
System. out. println ("Thread2 is sleeping ten millisecond ...");
Try {
Thread. Sleep (10);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
System. out. println ("Thread2 are going on ...");
System. out. println ("Thread2 is being over!");
}
}
}
}
The difference between sleep and wait