The waiting-wake mechanism of producer and consumer in Java Multi-threading @version2.0

Source: Internet
Author: User

Second, the producer consumer model student class member variable production and consumption demo,

@Version2.0

Add a synchronization method to the student class: Synchronized get () consumer, synchronized set () producer

In the final version of the code: the student member variable was privatized,
The functions of setting and acquiring are encapsulated and synchronized,
The set or fetch thread only needs to invoke the method.

1. Wait for wake-up:
There are three methods available in the object class:
Wait (): Wait
Notify (): Wake up a single thread
Notifyall (): Wake All Threads
//==========================

The first is the key student class object

1  Public classStudent {2     PrivateString name;3     Private intAge ;4     Private BooleanFlag;5 6     //Synchronization Method7      Public synchronized voidSet (String name,intAge ) {8         //If you have data, wait .9         if( This. Flag) {Ten             Try { One                  This. Wait (); A}Catch(interruptedexception e) { - e.printstacktrace (); -             } the         } -  -         //Setting up data -          This. Name =name; +          This. Age =Age ; -  +         //Once you have the data modification tag, and wake up A          This. Flag =true; at          This. Notify (); -     } -  -     //Synchronous Fetch Method -      Public synchronized voidget () { -         //wait if there's no data in         if(! This. Flag) { -             Try { to                  This. Wait (); +}Catch(interruptedexception e) { - e.printstacktrace (); the             } *         } $         //Get DataPanax NotoginsengSystem.out.println ( This. Name + "---" + This. age); -         //after getting to the data, the modification is marked false and finally wakes up.  the          This. Flag =false; +          This. Notify (); A}

Producer of threads, this time super simple ~~~

1  Public classSetthreadImplementsRunnable {2     PrivateStudent S;3     Private intx = 0;4 5      PublicSetthread (Student s) {6          This. S =s;7     }8 9 @OverrideTen      Public voidrun () { One          while(true) { A  -             if(x% 2 = = 0) { -S.set ("Java", 30); the}Else { -S.set ("Android", 20); -             } -X + +; +         } -  +     } A  at}

Consumer's thread, this time super simple ~~~

1  Public classGetThreadImplementsRunnable {2     PrivateStudent S;3 4      PublicGetThread (Student s) {5          This. S =s;6     }7 8      Public voidrun () {9          while(true) {Ten s.get (); One         } A     } -}

Consumer's thread, this time super simple ~~~

1  Public classDemo {2      Public Static voidMain (string[] args) {3         //shared data, created externally, as parameters, by constructing common4Student s =NewStudent ();5         //using the same parameter in a construct6Setthread st =NewSetthread (s);7GetThread GT =NewGetThread (s);8 9Thread T1 =NewThread (ST);//Setting up dataTenThread t2 =NewThread (GT);//Get Data One  A T2.start (); - T1.start (); -     } the}

The waiting-wake mechanism of producer and consumer in Java Multi-threading @version2.0

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.