First, using the While method to implement communication between threads
PackageCom.ietree.multithread.sync;Importjava.util.ArrayList;Importjava.util.List; Public classMyList {Private volatile StaticList List =NewArrayList (); Public voidAdd () {List.add ("Apple"); } Public intsize () {returnlist.size (); } Public Static voidMain (string[] args) {FinalMyList List1 =NewMyList (); Thread T1=NewThread (NewRunnable () {@Override Public voidrun () {Try { for(inti = 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=NewThread (NewRunnable () {@Override Public voidrun () { while(true) { if(list1.size () = = 5) {System.out.println ("Current thread received notification:" + thread.currentthread (). GetName () + "List size = 5 thread stop ..."); Throw Newruntimeexception (); } } } }, "T2"); T1.start (); T2.start (); }}
Program output:
"T2" The current thread is notified: T2 list size = 5 thread stops. Java.lang.RuntimeException at com.ietree.multithread.sync.mylist$2.run (mylist.java:43) at Java.lang.Thread.run (Unknown Source) Current thread: T1 added an element. Current thread: T1 an element has been added ... Current thread: T1 an element has been added ... Current thread: T1 an element has been added ... Current thread: T1 an element has been added ...
Understanding: Thread Thread2 continuously detects whether this condition (list.size () ==5) is true through the while statement, thus enabling communication between threads. But this approach wastes CPU resources.
Second, wait Notfiy method realizes communication between threads of multi-thread
Using this approach to implement thread communication requires that thewait and notify must be used with the Synchronized keyword, the wait method releases the lock, and the Notify method does not release the lock. And in this case, the Thread2 must be executed first.
PackageCom.ietree.multithread.sync;Importjava.util.ArrayList;Importjava.util.List; Public classLISTADD3 {Private volatile StaticList List =NewArrayList (); Public voidAdd () {List.add ("Apple"); } Public intsize () {returnlist.size (); } Public Static voidMain (string[] args) {FinalLISTADD2 List2 =NewListAdd2 (); //1 instantiation of a lock//when using wait and notify, be sure to work with the Synchronized keyword FinalObject lock =NewObject (); Thread T1=NewThread (NewRunnable () {@Override Public voidrun () {Try { synchronized(lock) { for(inti = 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 ("Notification has been issued.");
Do not release the lock, when encountering size=5 or continue to execute lock.notify (); } } } } Catch(interruptedexception e) {e.printstacktrace (); } } }, "T1"); Thread T2=NewThread (NewRunnable () {@Override Public voidrun () {synchronized(lock) {if(List2.size ()! = 5) { Try {
Release the lock to allow other threads to execute lock.wait (); } Catch(interruptedexception e) {e.printstacktrace (); }} System.out.println ("Current thread:" + thread.currentthread (). GetName () + "receive notification thread stop ..."); Throw Newruntimeexception (); } } }, "T2"); T2.start (); T1.start (); }}
Program output:
"T2" java.lang.RuntimeException at com.ietree.multithread.sync.listadd3$2.run (listadd3.java:59) At Java.lang.Thread.run (Unknown Source)
Third, using the Countdownlatch class to achieve real-time communication between multi-thread threads
PackageCom.ietree.multithread.sync;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.concurrent.CountDownLatch; Public classLISTADD2 {Private volatile StaticList List =NewArrayList (); Public voidAdd () {List.add ("Apple"); } Public intsize () {returnlist.size (); } Public Static voidMain (string[] args) {FinalLISTADD2 List2 =NewListAdd2 (); FinalCountdownlatch Countdownlatch =NewCountdownlatch (1); Thread T1=NewThread (NewRunnable () {@Override Public voidrun () {Try { for(inti = 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 ("Notification has been issued."); Countdownlatch.countdown (); } } // }}Catch(interruptedexception e) {e.printstacktrace (); } } }, "T1"); Thread T2=NewThread (NewRunnable () {@Override Public voidrun () {if(List2.size ()! = 5) { Try{countdownlatch.await (); } Catch(interruptedexception e) {e.printstacktrace (); }} System.out.println ("Current thread:" + thread.currentthread (). GetName () + "receive notification thread stop ..."); Throw Newruntimeexception (); } }, "T2"); T2.start (); T1.start (); }}
Program output:
"T2" Current thread: T1 an element was added ... Current thread: T2 received notification thread stopped. Java.lang.RuntimeException at com.ietree.multithread.sync.listadd2$2.run (listadd2.java:56) at Java.lang.Thread.run (Unknown Source) Current thread: T1 added an element. Current thread: T1 an element has been added ... Current thread: T1 an element has been added ... Current thread: T1 an element has been added ...
Communication between threads in a Java multi-threading thread