Tag:java thread thread object
(online answer: sleep is a threading class ( thread sleep wait is object wait method causes this thread to discard the object lock, enter the waiting lock pool waiting for this object, and only emit notify method (or notifyall ) After this thread enters object lock pool ready to get object lock into run state. )
Sleepis that the thread that is executing is actively surrenderingCPU,CPUto execute other threads, inSleepafter the specified time,CPUwill go back to this thread and continue down, if the current thread has entered a sync lock,Sleepmethod does not release the lock, even if the current thread uses theSleepmethod to give upCPU, but other threads that are blocked by a synchronous lock cannot be executed. waitin a thread that has entered a sync lock, let itself temporarily give up the sync lock so that other threads waiting for the lock can get a synchronous lock and run, and only other threads call theNotifyMethod (Notifydo not release the lock, just tell the callwaitthe thread of the method can go into 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 yet released. IfNotifyThere is a lot of code behind the method that requires the code to be executed before the lock is released and can benotfiymethod After adding a wait and some code to see the effect), callwaitmethod will dismiss the thread of thewaitthe state and the program can be locked again after continuing to run down. Forwaitthe explanation must cooperate with the example code to explain, only then appears oneself really to understand.
Package Com.huawei.interview;public class Multithread {public static void main (string[] args) {new Thread (new Thread1 ()). Start (); try {thread.sleep);} catch (Interruptedexception e) {e.printstacktrace ();} New Thread (New Thread2 ()). Start (); private static class Thread1 implements Runnable {@Overridepublic void run () {synchronized (multithread.class) {System.ou T.println ("Enter thread1 ..."); System.out.println ("Thread1 is Waiting"); try {MultiThread.class.wait ();
/*[release a lock in two ways, the first is that the program naturally leaves the scope of the monitor, that is, the code range that leaves the Synchronized keyword, and another way to invoke the monitor object's wait method within the code that governs the synchronized keyword. Here, use the Wait method to release the lock. */} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread1 is going on ..."); System.out.println ("Thread1 is being over!");}
/* Because 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. */}}private Static class Thread2 implements Runnable {@Overridepublic void run () {synchronized (multithread.class) {Syste M.out.println ("Enter thread2 ..."); System.out.println ("Thread2 notify other thread can release wait status."); MultiThread.class.notify ();
/* 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. */system.out.println ("Thread2 is sleeping ten millisecond ..."); try {thread.sleep (10);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Thread2 is going on ..."); System.out.println ("Thread2 is being over!");}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
What is the difference between sleep () and wait () in Java?