For the interview, child thread 2 executes 10 sub-threads 2 execution 20 times 100 times with the main thread to loop the execution 50 times public class interview {public static Void main (String[] args) {final business b = new business ();new Thread (New runnable () {@Overridepublic void run () {for (int i = 1; i <= 50; i++) {b.sub2 (i);}}). Start (); New thread (New runnable () {@Overridepublic void run () {for (int i = 1; i <= 50; i++) {b.sub3 (i);}}). Start ();for (int i = 1; i <= 50; i++) {b.main (i);}} class business{private int order=1; Lock lock = new reentrantlock (); Condition con1 = lock.newcondition (); Condition con2 = lock.newcondition (); Condition con3 = lock.newcondition ();p ublic void sub2 (int i) {lock.lock (); try {while (Order&NBSP;!=&NBSP;2) {try {con2.await ();} catch (interruptedexception e) {e.printstacktrace ();}} for (int j = 1; j <= 10; j++) {system.out.println ("sub2 thread is sequence of " + j+ ". lamp of " + i);} System.out.println ("/... ............"); order = 3;con3.signal (); finally{lock.unlock ();}} PUBLIC&NBSP;VOID&NBSP;SUB3 (int i) {Lock.lock ();try {while (order != 3) {try {con3.await ();} catch (interruptedexception e) {e.printstacktrace ();}} for (int j = 1; j <= 20; j++) {system.out.println ("sub3 thread is sequence of " + j+ ". lamp of " + i);} System.out.println ("/... ............"); order = 1;con1.signal (); finally{lock.unlock ();}} Public void main (int i) {LOck.lock ();try {while (order != 1) {try {con1.await ();} catch (interruptedexception e) {e.printstacktrace ();}} for (int j = 1; j <= 100; j++) {system.out.println ("main thread is sequence of " + j+ ". lamp of " + i);} order = 2; System.out.println ("...................... finally {lock.unlock ();}}}
This article comes from "either desperately, or roll back!" "Blog, be sure to keep this provenance http://jiangzuun2014.blog.51cto.com/8732469/1538354