First we are based on grooming our previous android (Java) Learning note 70 about producer and consumer program ideas:
Here we will focus on this wait-and-wake mechanism :
The first step is to show the wait-wake mechanism through code first
Packagecn.itcast_05;/** Analysis: * Resource class: Student * Set student data: Setthread (producer) * Get Student Data: getthread (consumer) * Test class: Studentdemo * * Question 1: Write the code according to the idea, the discovery data each time is: null---0 * Reason: we create a new resource in each thread, and we need to set and get the thread's resources should be the same * how to implement it? * This data is created by the outside world and passed to other classes through the construction method. * * Question 2: In order to make the data better, I added loops and judgments, gave different values, and this time created a new problem * A: multiple occurrences of the same data * B: Name and age mismatch * Reason: * A: The same data appears multiple times * CPU a little time slice of execution right, is enough for you to execute many times. * B: Name and age mismatch * thread-run randomness * thread safety issues: * A: Whether the multithreaded environment is * B: whether there is shared data is * C: Whether there are multiple statements to manipulate shared data is a * solution: * plus lock. * Note: * A: Different types of threads are to be locked. * B: The locks of different kinds of wire loads must be the same. * *Question 3: Although the data is safe, but, once a large and not good-looking, I would like to sequentially one output. * How to achieve it? *solved by the wait-wake mechanism provided by Java. * * Wait for Wake up: * Three methods are available in the object class: *Wait (): Wait*notify (): Wake up a single thread*Notifyall (): Wake All Threads* Why are these methods not defined in the thread class? * Calls to these methods must be called through the lock object, and the lock object we just used is an arbitrary lock object. * Therefore, these methods must be defined in the object class.*/ Public classStudentdemo { Public Static voidMain (string[] args) {//Create a resourceStudent s =NewStudent (); //sets and gets the classSetthread st =NewSetthread (s); GetThread GT=NewGetThread (s); //Thread ClassThread T1 =NewThread (ST); Thread T2=NewThread (GT); //Start ThreadT1.start (); T2.start (); }}
1 Packagecn.itcast_05;2 3 Public classSetthreadImplementsRunnable {4 5 PrivateStudent S;6 Private intx = 0;7 8 PublicSetthread (Student s) {9 This. S =s;Ten } One A @Override - Public voidrun () { - while(true) { the synchronized(s) { - //judged there was no - if(s.flag) { - Try { +S.wait ();//T1 Wait, release the lock. -}Catch(interruptedexception e) { + e.printstacktrace (); A } at } - - if(x% 2 = = 0) { -S.name = "Brigitte"; -S.age = 27; -}Else { inS.name = "Elina"; -S.age = 30; to } +x + +;//x=1 - the //Modify Tags *S.flag =true; $ //wake-Up threadPanax NotoginsengS.notify ();//Wake up T2, wake up does not mean that you can execute immediately, you must also grab the CPU execution rights. - } the //t1 have, or T2 have + } A } the}
1 Packagecn.itcast_05;2 3 Public classGetThreadImplementsRunnable {4 PrivateStudent S;5 6 PublicGetThread (Student s) {7 This. S =s;8 }9 Ten @Override One Public voidrun () { A while(true) { - synchronized(s) { - if(!S.flag) { the Try { -S.wait ();//T2 was waiting. Release the lock immediately. When I wake up in the future, I wake up from here. -}Catch(interruptedexception e) { - e.printstacktrace (); + } - } + ASystem.out.println (s.name + "---" +s.age); at //Brigitte--- - //Elina--- - - //Modify Tags -S.flag =false; - //wake-Up thread inS.notify ();//Wake T1 - } to } + } -}
1 Package cn.itcast_05; 2 3 Public class Student {4 String name; 5 int Age ; 6 Boolean // By default, there is no data, and the default is False, if true, indicating that there is data 7 }
Android (Java) Learning Note 71: Waiting wake-up mechanism for producers and consumers