Thread Communication:
If thread A and thread B hold objects of the same MyObject class object, the two threads will call different methods, but they are executed synchronously, for example: thread B needs to wait for thread A to execute the MethodA () method before it can execute the MethodB () method. This enables both thread A and thread B to communicate.
methods to use in thread communication: theWait () method:
The thread of the method that performs the synchronous lock (obj object) enters a blocked state, releasing the lock on the object, which the Java virtual machine places into the waiting pool of the object, which, if it is to be executed again, requires another thread to wake it.
1 Packagetongxin;2 3 Public classMythreadaextendsThread {4 PrivateObject obj;5 PublicMythreada (String name,object obj) {6 7 Super(name);8 This. obj =obj;9 Ten } One Public voidrun () { A synchronized(obj) {//set obj as a synchronous lock - - for(inti = 0;i<10;i++){ theSystem.out.println (Thread.CurrentThread (). GetName () + "---" +i); - - if(i = = 5){ - Try { + obj.wait (); -}Catch(interruptedexception e) { + //TODO auto-generated Catch block A e.printstacktrace (); at } - Try { -Sleep (100); -}Catch(interruptedexception e) { - //TODO auto-generated Catch block - e.printstacktrace (); in } - } to } + } - } the *}
1 Packagetongxin;2 3 Public classDemo01 {4 5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7 8Object obj =NewObject ();9Mythreada MT1 =NewMythreada ("A", obj);Ten Mt1.start (); One A } - -}
Output
1 a---02 a---13 a---24 a---35 a---46 a---5
When i = 5 o'clock, the Wait () method for the synchronous lock is executed, and thread a releases the synchronization lock into the blocked state, and if the a thread has resumed running, another thread that holds the same synchronization lock is required to wake it.
Ways to wake Up threads: Notifi ()
The Notifi method in which another thread executes a synchronous lock while it is running wakes up a thread waiting in the object's waiting pool (randomly selected), and the Java Virtual Opportunity randomly chooses a thread Go to the lock pool of the object.
Note that one thread is going to wake up another thread that executes the wait () method into the blocked state, and it is going to execute the Notifi () method of the same synchronous lock.
If there is a room, a in the door after the lock, a in the room asleep, b to wake up, will use the lock (synchronous lock) on the key to open the door. A and B use the same lock, except that A is locked (wait () method), B is open (Notifi () method).
1 Packagetongxin;2 3 Public classMythreadbextendsThread {4 PrivateObject obj;5 Publicmythreadb (String name,object obj) {6 7 Super(name);8 This. obj =obj;9 Ten } One Public voidrun () { A synchronized(obj) {//set obj as a synchronous lock - - for(inti = 0;i<10;i++){ theSystem.out.println (Thread.CurrentThread (). GetName () + "---" +i); - - if(i = = 2){ - obj.notify (); + Try { -Sleep (100); +}Catch(interruptedexception e) { A //TODO auto-generated Catch block at e.printstacktrace (); - } - } - } - } - } in - to}
1 Packagetongxin;2 3 Public classDemo01 {4 5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7 8Object obj =NewObject ();9Mythreada MT1 =NewMythreada ("A", obj);TenMYTHREADB mt2 =NewMYTHREADB ("B", obj); One Mt1.start (); A Mt2.start (); - - the } - -}
Output
A---0a---1a---2a---3a---4a---5b---0b---1b ---2b---3b---4b---5b---6b---7b---8b ---9a---6a---7A---8A---9
Create a new Thread object B to wake up a thread, run the process, I = 2 o'clock, B to wake up a thread to go to the object's lock pool, but at this point the B thread continues to run because B holds the synchronization lock, and when the run finishes releasing the sync lock, a gets to the sync lock recovery run.
Notifyall () Method:
This method wakes all threads in the lock pool of this object.
If there is something wrong, please correct it, thank you.
javase--Thread Communication