The first explanation:functionally, they are used for threading control, and their most essential difference is that sleep () does not release the sync lock, and wait () releases the sync lock.
There is a difference in usage: sleep (milliseconds) can be specified in time to make him automatically wake up, if the time is less than you can only call Interreput () to forcibly interrupt, wait () can be directly aroused with notify ().
The second explanation:
Sleep is a static method of the thread class. The function of sleep is to allow the thread to hibernate the time it is set to resume when the time arrives, that is, sleep will arrive at the time the event is reached to resume the thread execution, for example:
New Thread () {@Overridepublic void run () {try {System.out.println ("Child thread Hibernation ——————————————————————————————————————"); Thread.Sleep (5000); SYSTEM.OUT.PRINTLN ("Child threads continue to ——————————————————————————————————————");} catch (Interruptedexception e) {e.printstacktrace ();}}}. Start (); int i = 0;while (true) {try {System.out.println ("main thread hibernate" + i++); Thread.Sleep (1000); System.out.println (New Date (). getseconds ()); System.out.println ("Main thread continuation");} catch (Interruptedexception e) {e.printstacktrace ();}}
Wait is the method of object, which means that you can call the Wait method on any object, and calling the wait method will suspend the caller's thread until the Notify method of the same object is called by another thread to reactivate the caller (or wait time, but this usage is rare). For example:
New Thread () {@Overridepublic void run () {try {synchronized (obj.class) {System.out.println ("child-thread hibernation ———————————————————— —————————————————— "); Obj.class.wait (); SYSTEM.OUT.PRINTLN ("Child threads continue to ——————————————————————————————————————");}} catch (Interruptedexception e) {e.printstacktrace ();}}}. Start (); int i = 0;synchronized (Obj.class) {while (I! = 5) {i++; System.out.println (New Date (). getseconds ());} System.out.println ("Wake Up"); Obj.class.notify ();}
Console output:
The child thread sleeps —————————————————————————————————————— 5152535454 wakes the child thread to continue ——————————————————————————————————————
The third explanation:There is an essential difference between the two of the perpetrators.
Sleep () is a time when a thread is paused for a period, and its control is determined by the current thread, that is, in the threads. Well, for example, what I'm going to do is "fire-water-boil noodles", and when I finish the fire, I don't boil water immediately. I'm going to take some time off to burn. The initiative for running is controlled by my process.
and wait (), first of all, this is called by a certain object, the object is understood as a messenger, when the person in a thread said "Pause!", is also thisobj.wait (), where the suspension is blocked, or "ignition--water boil rice", Thisobj is like a person who oversees me standing next to me, originally the thread should execute 1 after execution 2, then execute 3, and at 2 is called by that object pause, then I will always wait here and not execute 3, but this process is not finished, I always want to cook, but not allowed, Until the object says "notify the suspended thread to start!", which is thisobj.notify (), then I can cook, and the suspended thread resumes execution from the pause.
In fact, both can let the thread pause for a period of time, but the essential difference is that one thread is running state control, and one is the problem of communication between threads.
In the Java.lang.Thread class, sleep () is provided, while the Wait (), notify (), and Notifyall () methods are available in the Java.lang.Object class to manipulate the thread. Sleep () sleeps a thread, and the parameter can specify a time.
Wait () can suspend a thread until it times out or the thread wakes up. Wait has two forms of wait () and wait (milliseconds).
The differences between sleep and wait are:
1, the two methods come from different classes, namely thread and object.
2, mostly the sleep method does not release the lock, and the wait method frees the lock so that other threads can use the synchronization control block or method.
3,wait,notify and Notifyall can only be used in synchronous control methods or in sync control blocks, and sleep can be used anywhere.
4,sleep must catch exceptions, while wait,notify and notifyall do not need to catch exceptions.
Interpretation of sleep () and wait () in Java