Synchronous threads, production consumption cases.

Source: Internet
Author: User

 Public classShareresourse {PrivateString name; PrivateString Gender; Private Booleanisempty=true;//indicates that the data is empty    /*** Producer stores data in shared resources * Name storage names * Gender stored gender*/         synchronized  Public voidpush (String name,string gender) {Try {              while(!isempty) {//current is false, storage is not empty, waiting for consumer consumption,//called using a synchronous lock object, which means that the current thread releases the synchronization lock and can only be awakened by other threads                  This. Wait (); }              //~~~~~~ began to consume ~~~~~~~             This. Name =name; Thread.Sleep (10);  This. gender=gender; //~~~~~~ began to consume ~~~~~~~~isempty=false;//not empty needs a consumer spending             This. Notify ();//Wake up a consumer}Catch(Exception e) {e.printstacktrace (); }     }        /*** Consumers remove data from shared resources*/     synchronized  Public voidpopup () {Try {              while(IsEmpty) {//current is true, storage is empty, waiting for producer to produce,//called using a synchronous lock object, which means that the current thread releases the synchronization lock and can only be awakened by other threads                 This. Wait (); }                //~~~~~~~~ started production of ~~~~~~~~~~~~~Thread.Sleep (10); System.out.println ( This. Name + "--" + This. Gender); //~~~~~~~~ end of production ~~~~~~~IsEmpty =true;//is empty and needs to be produced                 This. Notify ();//Wake up a producer}Catch(Exception e) {e.printstacktrace (); }     }}

Producers

//producers Public classProductImplementsRunnable {//Shared resource Objects    PrivateShareresourse Resourse =NULL;  PublicProduct (Shareresourse resourse) { This. Resourse =Resourse; }     Public voidrun () { for(inti = 0; I < 500; i++) {            if(i%2==0) Resourse.push ("Brother Chun ~", "male"); ElseResourse.push ("Sister Feng", "female"); }    }}

Consumers

 Packagethread communication; Public classConsumerImplementsRunnable {PrivateShareresourse resourse=NULL;  PublicConsumer (Shareresourse resourse) {//using the constructor to pass Resourse         This. resourse=Resourse; } @Override Public voidrun () { for(inti = 0; I < 500; i++) {resourse.popup (); }    }}

Test class

 package   thread communication;  public  class   App { public  static  void   main (string[] args) {shareresourse resourse  = ne        W   Shareresourse ();  new  Thread (new  Product (resourse)) . Start (); //         new  Thread (new  Consumer (resourse)). Start (); //  }}  

Synchronous threads, production consumption cases.

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.