Multithreading wait and notify implementations 1212

Source: Internet
Author: User

 package   Threadt;  public  class   ThreadMain { public  static  void   main (String args[]) { /span>final  Object obj = new  Object (); //  new
       Thread (new  thread1to2 (1, obj), "xiaoming"  new  Thread (new  thread1to2 (2, obj),    "Xiao Wang" ). Start (); }}
 PackageThreadt; Public classThread1to2ImplementsRunnable {Private Final intNumber ; Private intCount = 10;  PublicObject obj;  PublicThread1to2 (intNumber , Object obj) {         This. obj =obj;  This. Number =Number ; }     Public voidrun () {synchronized(obj) { while(count--> 0) {                Try{obj.notify ();//wakes the thread that waits for the RES resource, and gives the lock to the thread (the sync lock completes the auto-release lock)System.out.println (Thread.CurrentThread (). GetName () + "shout:" +Number ); if(Count > 0) obj.wait ();//release the CPU control, release the res lock, this thread is blocked, waiting to be awakened. //If you end to release the lock, it will block the process                    Else if(count = = 0) obj.notify (); } Catch(interruptedexception e) {e.printstacktrace (); }            }        }    }}

Multithreading wait and notify implementations 1212

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.