JavaSE -- thread communication, javase thread

Source: Internet
Author: User

JavaSE -- thread communication, javase thread

Thread communication:

If thread A and thread B hold the object of the same MyObject class, the two threads will call different methods, but they are executed synchronously, for example: thread B needs to wait until thread A finishes executing the methodA () method before it can execute the methodB () method. In this way, the communication between thread A and thread B is realized.

Method Used in thread communication: wait () method:

When the thread of this method that executes the synchronization lock (obj object) enters the blocking state, the lock of the object will be released. The Java Virtual Machine places the thread in the waiting pool of the object. If the thread needs to be executed again, other threads need to wake it up.

 

1 package tongxin; 2 3 public class MyThreadA extends Thread {4 private Object obj; 5 public MyThreadA (String name, Object obj) {6 7 super (name); 8 this. obj = obj; 9 10} 11 public void run () {12 synchronized (obj) {// set obj to synchronization lock 13 14 for (int I = 0; I <10; I ++) {15 System. out. println (Thread. currentThread (). getName () + "---" + I); 16 17 if (I = 5) {18 try {19 obj. wait (); 20} catch (InterruptedException e) {21 // TODO Auto-generated catch block22 e. printStackTrace (); 23} 24 try {25 sleep (100); 26} catch (InterruptedException e) {27 // TODO Auto-generated catch block28 e. printStackTrace (); 29} 30} 31} 32} 33} 34 35}

 

 1 package tongxin; 2  3 public class Demo01 { 4  5     public static void main(String[] args) { 6         // TODO Auto-generated method stub 7          8         Object obj = new Object(); 9         MyThreadA mt1 = new MyThreadA("A",obj);10         mt1.start();11 12     }13 14 }

Output:

1 A---02 A---13 A---24 A---35 A---46 A---5

When I = 5, the wait () method of the synchronization lock is executed, and thread A releases the synchronization lock into the blocking state. If thread A still resumes running, then, another thread holding the same synchronization lock needs to wake it up.

 

Method to wake up a thread: notifi ()

When another thread executes the Synchronous lock notifi method during running, it will wake up a thread waiting in the waiting pool of the object (randomly selected ), the Java Virtual Machine randomly chooses a thread to go to the lock pool of the object.

Note that if one thread needs to wake up another thread that executes the wait () method to enter the blocked state, it needs to execute the notifi () method of the same synchronization lock.

If there is A room, A will lock the door and A will fall asleep in the room. If B wants to wake up A, it will use the key on the lock (Synchronous lock) to open the door. A and B use the same lock, except that A is locking the door (wait () method) and B is opening the door (notifi () method ).

1 package tongxin; 2 3 public class MyThreadB extends Thread {4 private Object obj; 5 public MyThreadB (String name, Object obj) {6 7 super (name); 8 this. obj = obj; 9 10} 11 public void run () {12 synchronized (obj) {// set obj to synchronization lock 13 14 for (int I = 0; I <10; I ++) {15 System. out. println (Thread. currentThread (). getName () + "---" + I); 16 17 if (I = 2) {18 obj. running y (); 19 try {20 sleep (100); 21} catch (InterruptedException e) {22 // TODO Auto-generated catch block23 e. printStackTrace (); 24} 25} 26} 27} 28} 29 30 31}
 1 package tongxin; 2  3 public class Demo01 { 4  5     public static void main(String[] args) { 6         // TODO Auto-generated method stub 7          8         Object obj = new Object(); 9         MyThreadA mt1 = new MyThreadA("A",obj);10         MyThreadB mt2 = new MyThreadB("B",obj);11         mt1.start();12         mt2.start();13         14 15     }16 17 }

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 thread A. During the running process, when I = 2, thread B wakes up thread A and forwards it to the lock pool of the object, but at this time, because B holds the synchronization lock, line B continues to run. After the operation ends, the synchronization lock is released. At this time, A obtains the synchronization lock and resumes running.

 

NotifyAll () method:

This method will wake up all threads in this object lock pool.

 

If anything is wrong, please correct me. Thank you.

 

Related Article

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.