This article can be used as a learning note for the Zhang Xiaoxiang-java multi-threading and concurrency Library advanced application.
Above we said lock, that is a more object-oriented substitution of synchronized, in the original synchronized, we can call the object's wait and notify method, then use lock, how to thread communication.
A friend who is not clear about the lock can see
http://blog.csdn.net/dlf123321/article/details/42919085
The answer is condition.
Condition on the one hand is the addition of the lock function (that is, you use lock, in order to ensure the communication of the thread, you have to use condition)
On the other hand, synchronized's notifyall is waking up all the waiting threads, so if some threads I don't want to wake up.
Look at the following example
The main thread runs 10 times, then the child thread 2 runs 20 times, then the child thread 3 runs 30 times
The whole above runs 4 times.
We analyze that after the main thread has run 10 times, the following one is blocking itself, while the child thread 2 should be woken up, and the child thread 3 cannot be awakened.
If you use Notifyall, you wake up thread 2 and thread 3.
Look at the method
The usual is await and signal.
The two just correspond to the wait and notify of object.
Let's look at the code:
Package Cn.itcast.heima2;import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.Lock; Import Java.util.concurrent.locks.reentrantlock;public class Threeconditioncommunication {/** * @param args */public static void Main (string[] args) {final business business = new Business (); New Thread (New Runnable () {@Overridepublic void Run () {for (int i=1;i<=4;i++) {business.sub2 (i);}}}). Start (); New Thread (New Runnable () {@Overridepublic void Run () {for (int i=1;i<=4;i++) {business.sub3 (i);}}). Start (); for (int i=1;i<=4;i++) {business.main (i);}} Static class Business {lock lock = new Reentrantlock (); Condition condition1 = Lock.newcondition (); Condition condition2 = Lock.newcondition (); Condition Condition3 = Lock.newcondition (); private int shouldsub = 1; public void sub2 (int i) {lock.lock (); try{while (shouldsub! = 2) {try {condition2.await ()} catch (Exception e) {//TODO auto-generated catch Blocke.printstac Ktrace ();} }for (int j=1;j<=20;j++) {System.out.println ("sUB2 thread sequence of "+ j +", loop of "+ i);} Shouldsub = 3; Condition3.signal (); }finally{Lock.unlock (); }} public void sub3 (int i) {lock.lock (); try{while (shouldsub! = 3) {try {condition3.await ()} catch (Exception e) {//TODO auto-generated catch Blocke.printstac Ktrace ();} }for (int j=1;j<=30;j++) {System.out.println ("SUB3 thread sequence of" + j + ", loop of" + i);} Shouldsub = 1; Condition1.signal (); }finally{Lock.unlock (); }} public void main (int i) {lock.lock (); try{while (shouldsub! = 1) {try {condition1.await ()} catch (Exception e) {//TODO auto-generated catch Blocke.printstack Trace ();} }for (int j=1;j<=10;j++) {System.out.println ("main thread sequence of" + j + ", loop of" + i);} Shouldsub = 2;condition2.signal (); }finally{Lock.unlock (); } }}}
Talk about condition.