Communication between threads in Java multithreading, java Multithreading

Source: Internet
Author: User

Communication between threads in Java multithreading, java Multithreading
1. Use the while method to implement inter-thread Communication

Package com. ietree. multithread. sync; import java. util. arrayList; import java. util. list; public class MyList {private volatile static List list = new ArrayList (); public void add () {list. add ("apple");} public int size () {return list. size () ;}public static void main (String [] args) {final MyList list1 = new MyList (); Thread t1 = new Thread (new Runnable () {@ Override public void run () {try {for (int I = 0; I <10; I ++) {list1.add (); System. out. println ("current Thread:" + Thread. currentThread (). getName () + "added an element .. "); Thread. sleep (500) ;}} catch (InterruptedException e) {e. printStackTrace () ;}}, "t1"); Thread t2 = new Thread (new Runnable () {@ Override public void run () {while (true) {if (list1.size () = 5) {System. out. println ("notification received by the current Thread:" + Thread. currentThread (). getName () + "list size = 5 threads stopped .. "); throw new RuntimeException () ;}}," t2 "); t1.start (); t2.start ();}}

Program output:

Current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. exception in thread "t2" the current thread receives a notification: t2 list size = 5 threads stopped .. java. lang. runtimeException at com. ietree. multithread. sync. myList $ 2.run( MyList. java: 43) at java. lang. thread. run (Unknown Source) current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element ..

Understanding: thread Thread2 constantly checks whether the condition (list. size () = 5) is true through the while statement, thus realizing inter-thread communication. However, this method wastes CPU resources.

II. The wait notfiy method enables communication between threads in multiple threads

To use this method to implement thread communication, note that wait and notify must be used with the synchronized keyword. The wait method releases the lock, and the notify method does not release the lock. In this example, Thread2 must be executed first.

Package com. ietree. multithread. sync; import java. util. arrayList; import java. util. list; public class ListAdd3 {private volatile static List list = new ArrayList (); public void add () {list. add ("apple");} public int size () {return list. size ();} public static void main (String [] args) {final ListAdd2 list2 = new ListAdd2 (); // 1 instantiate a lock. // when using wait and ipvy, use the final Object lock = new Object () with the synchronized keyword (); thread t1 = new Thread (new Runnable () {@ Override public void run () {try {synchronized (lock) {for (int I = 0; I <10; I ++) {list2.add (); System. out. println ("current Thread:" + Thread. currentThread (). getName () + "added an element .. "); Thread. sleep (500); if (list2.size () = 5) {System. out. println ("a notification has been sent .. ");
// Do not release the lock. When the size is 5, continue to execute the lock. policy () ;}}} catch (InterruptedException e) {e. printStackTrace () ;}}, "t1"); Thread t2 = new Thread (new Runnable () {@ Override public void run () {synchronized (lock) {if (list2.size ()! = 5) {try {
// Release the lock so that other threads can execute the lock. wait ();} catch (InterruptedException e) {e. printStackTrace () ;}} System. out. println ("current Thread:" + Thread. currentThread (). getName () + "the notification thread is stopped .. "); throw new RuntimeException () ;}}," t2 "); t2.start (); t1.start ();}}

Program output:

Current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. A notification has been sent .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t2 receives the notification thread to stop .. exception in thread "t2" java. lang. runtimeException at com. ietree. multithread. sync. listAdd3 $ 2.run( ListAdd3.java: 59) at java. lang. thread. run (Unknown Source)
Iii. Use the CountDownLatch class to implement real-time communication between threads in multiple threads
Package com. ietree. multithread. sync; import java. util. arrayList; import java. util. list; import java. util. concurrent. countDownLatch; public class ListAdd2 {private volatile static List list = new ArrayList (); public void add () {list. add ("apple");} public int size () {return list. size ();} public static void main (String [] args) {final ListAdd2 list2 = new ListAdd2 (); final CountDownLatch countDownLat Ch = new CountDownLatch (1); Thread t1 = new Thread (new Runnable () {@ Override public void run () {try {for (int I = 0; I <10; I ++) {list2.add (); System. out. println ("current Thread:" + Thread. currentThread (). getName () + "added an element .. "); Thread. sleep (500); if (list2.size () = 5) {System. out. println ("a notification has been sent .. "); countDownLatch. countDown () ;}///} catch (InterruptedException e) {e. printStackTrace ();}}}, "T1"); Thread t2 = new Thread (new Runnable () {@ Override public void run () {if (list2.size ()! = 5) {try {countDownLatch. await ();} catch (InterruptedException e) {e. printStackTrace () ;}} System. out. println ("current Thread:" + Thread. currentThread (). getName () + "the notification thread is stopped .. "); throw new RuntimeException () ;}," t2 "); t2.start (); t1.start ();}}

Program output:

Current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. A notification has been sent .. exception in thread "t2" current thread: t1 adds an element .. current thread: t2 receives the notification thread to stop .. java. lang. runtimeException at com. ietree. multithread. sync. listAdd2 $ 2.run( ListAdd2.java: 56) at java. lang. thread. run (Unknown Source) current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element .. current thread: t1 adds an element ..

 

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.