Java basic Knowledge Review Java Thread class learning (ix)--wait and notify differences

Source: Internet
Author: User

Wait and sleep differences:

    • The same point : Calling the Wait,sleep method can be a thread that goes into a blocking state, yielding the CPU's execution rights.
    • different points : 1.sleep You must specify a time, but the wait method can specify a time, or you can specify no time.

The 2.wait method must be used in synchronization, but sleep is not necessarily used in synchronization.

3. In synchronization, call the Sleep method to release the CPU execution, but does not release the lock, even if the CPU execution, other threads can not enter the synchronization lock, can not be executed. But the wait method not only frees CPU execution, but also releases the sync lock and goes into a blocking state. That is, the other thread waiting for this lock can get a synchronous lock and run, and the blocked thread will have to call the Notify method to wake up if it wants to get the CPU qualified again.

 PackageCom.lp.ecjtu.Thread; Public classWaitorsleep {Private Static  classThread1Implementsrunnable{@Override Public voidRun () {
Because the Thread1 and the following Thread2 inside the Run method will use the same object as a lock (monitor)
This cannot be used here because this is not the same object in the Thread2 inside this and Thread1
//Waitorsleep.class This bytecode object because it refers to the same object synchronized(Waitorsleep.class) {System.out.println ("Go to Thread 1----------"); System.out.println ("Thread 1----------Wait"); Try{waitorsleep.class. Wait ();//Release Lock}Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Thread 1 is running .... "); System.out.println ("End of Thread 1"); } } } Private Static classThread2Implementsrunnable{@Override Public voidrun () {synchronized(Waitorsleep.class) {System.out.println ("Go to Thread 2----------"); System.out.println ("Thread 2 wakes other threads ... "); Waitorsleep.class. Notify ();//Thread 2 wakes its other threads//since the notify does not release the lock, let thread 2 sleep 10 milliseconds, because the sleep method does not release the lock, so thread 1 does not execute//Thread 1 does not release the lock until thread 2 is finished executing Try{Thread.Sleep (10); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Thread 2 is running .... "); System.out.println ("End of Thread 2"); } } } /** * @paramargs*/ Public Static voidMain (string[] args) {NewThread (NewThread1 ()). Start (); NewThread (NewThread2 ()). Start (); }}

Output Result:

Enter thread 1----------
Thread 1----------Wait
Enter thread 2----------
Thread 2 wakes other threads ...
Thread 2 is running ....
Thread 2 Ends
Thread 1 is running ....
Thread 1 Ends
Everyone should have fully understood the difference ....

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.