java经典面试题:子线程先运行30次主线程,主线程40次,如此循环50次( 二 )

参见官方文档,Object (Java Platform SE 7 )synchronized (obj) { while (\u0026lt;condition does not hold\u0026gt;) obj.wait(); ... // Perform action appropriate to condition }和pthread系列是一样的pthread_mutex_lock(\u0026amp;mux);while (!should_fetch_task) { pthread_cond_wait(\u0026amp;cond, \u0026amp;mux);}pthread_mutex_unlock(\u0026amp;mux);
■网友
package test.example1;import java.util.concurrent.CountDownLatch;public class Tester1 {\tpublic static void main(String args) \t{\t\tfinal BusinessObj BusinessObj = new BusinessObj();\t\t\t\tfinal CountDownLatchWrapper latch_wrapper = new CountDownLatchWrapper();\t\tfinal int childCount = 5;\t\tfinal int parentCount = 10;\t\tfinal int totalCount = 20;\t\t\t\tThread parentThread = new Thread(new Runnable(){\t\t\tpublic void run() {\t\t\t\tfor (int i = 0; i \u0026lt; totalCount; i++) {\t\t\t\t\ttry {\t\t\t\t\t\tlatch_wrapper.getParent_latch().await();\t\t\t\t\t\tBusinessObj.doSomeThind(parentCount);\t\t\t\t\t\tlatch_wrapper.resetParent_latch();\t\t\t\t\t\tlatch_wrapper.getChild_latch().countDown();\t\t\t\t\t}\t\t\t\t\tcatch(Exception e) {\t\t\t\t\t\te.printStackTrace();\t\t\t\t\t}\t\t\t\t}\t\t\t}\t\t});\t\tparentThread.setName("ParentThread");\t\t\t\tThread childThread = new Thread(new Runnable(){\t\t\tpublic void run() {\t\t\t\tlatch_wrapper.getChild_latch().countDown();\t\t\t\tfor (int i = 0; i \u0026lt; totalCount; i++) {\t\t\t\t\ttry {\t\t\t\t\t\tlatch_wrapper.getChild_latch().await();\t\t\t\t\t\tBusinessObj.doSomeThind(childCount);\t\t\t\t\t\tlatch_wrapper.resetChild_latch();\t\t\t\t\t\tlatch_wrapper.getParent_latch().countDown();\t\t\t\t\t}\t\t\t\t\tcatch(Exception e) {\t\t\t\t\t\te.printStackTrace();\t\t\t\t\t}\t\t\t\t}\t\t\t}\t\t});\t\tchildThread.setName("ChildThread");\t\t\t\t\t\tchildThread.start();\t\tparentThread.start();\t}\tpublic static class BusinessObj \t{\t\tpublic void doSomeThind(int count) {\t\t\tfor (int i = 1; i \u0026lt;= count; i++) {\t\t\t\tSystem.out.println(String.format("by thread: %s: 【%d】", Thread.currentThread().getName(), i));\t\t\t}\t\t\tSystem.out.println("---------------------------------------------------------------------");\t\t}\t}\t\tpublic static class CountDownLatchWrapper \t{\t\tprivate CountDownLatch parent_latch = new CountDownLatch(1);\t\tprivate CountDownLatch child_latch = new CountDownLatch(1);\t\t\t\tpublic CountDownLatch getParent_latch() {\t\t\treturn parent_latch;\t\t}\t\t\t\tpublic CountDownLatch getChild_latch() {\t\t\treturn child_latch;\t\t}\t\t\t\tpublic void resetParent_latch() {\t\t\tparent_latch = new CountDownLatch(1); \t\t}\t\t\t\tpublic void resetChild_latch() {\t\t\tchild_latch = new CountDownLatch(1); \t\t}\t\t\t}}
■网友
/** * 2016年11月18日 */package jdk;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class MainSub50 {\tstatic volatile boolean isSubRun = true;\tstatic ReentrantLock lock = new ReentrantLock();\tstatic Condition subWait = lock.newCondition();\tstatic Condition mainWait = lock.newCondition(); \tpublic static void main(String args) throws InterruptedException {\t\tRunnable sub = ()-\u0026gt;{\t\tint times = 50;\t\t\twhile(times\u0026gt;0){\t\t\t\tlock.lock();\t\t\t\ttry{\t\t\t\t\twhile(!isSubRun){\t\t\t\t\t\t\tsubWait.await();\t\t\t\t\t}\t\t\t\t\tSystem.out.println("Sub 第 "+(50-times+1)+" 次");\t\t\t\t\tfor(int i = 0;i\u0026lt;10;i++){\t\t\t\t\t\tSystem.out.print("sub "+i+" ");\t\t\t\t\t}\t\t\t\t\tSystem.out.println();\t\t\t\t\tisSubRun = false;\t\t\t\t}catch(Exception e){\t\t\t\t\te.printStackTrace();\t\t\t\t}finally{\t\t\t\t\tmainWait.signalAll();\t\t\t\t\tlock.unlock();\t\t\t\t}\t\t\t\ttimes --;\t\t\t}\t\t};\t\tnew Thread(sub).start();\t\tmain();\t\t}\tstatic void main() throws InterruptedException{\t\tint times = 50;\t\twhile(times\u0026gt;0){\t\t\ttry{\t\t\t\tlock.lock();\t\t\t\twhile(isSubRun){\t\t\t\t\tmainWait.await();\t\t\t\t}\t\t\t\tSystem.out.println("main 第 "+(50-times+1)+" 次");\t\t\t\tfor(int i =0;i\u0026lt;10;i++){\t\t\t\t\tSystem.out.print("main "+i+" ");\t\t\t\t}\t\t\t\tSystem.out.println();\t\t\t\tisSubRun = true;\t\t\t}catch(Exception e){\t\t\t\te.printStackTrace();\t\t\t}finally{\t\t\t\tsubWait.signalAll();\t\t\t\tlock.unlock();\t\t\t}\t\t\ttimes -- ;\t\t}\t}}是这样吗?运行结果:Sub 第 1 次sub 0 sub 1 sub 2 sub 3 sub 4 sub 5 sub 6 sub 7 sub 8 sub 9 main 第 1 次main 0 main 1 main 2 main 3 main 4 main 5 main 6 main 7 main 8 main 9 Sub 第 2 次sub 0 sub 1 sub 2 sub 3 sub 4 sub 5 sub 6 sub 7 sub 8 sub 9 main 第 2 次main 0 main 1 main 2 main 3 main 4 main 5 main 6 main 7 main 8 main 9 Sub 第 3 次sub 0 sub 1 sub 2 sub 3 sub 4 sub 5 sub 6 sub 7 sub 8 sub 9 main 第 3 次main 0 main 1 main 2 main 3 main 4 main 5 main 6 main 7 main 8 main 9 Sub 第 4 次sub 0 sub 1 sub 2 sub 3 sub 4 sub 5 sub 6 sub 7 sub 8 sub 9 main 第 4 次main 0 main 1 main 2 main 3 main 4 main 5 main 6 main 7 main 8 main 9 Sub 第 5 次sub 0 sub 1 sub 2 sub 3 sub 4 sub 5 sub 6 sub 7 sub 8 sub 9 main 第 5 次.....后面都一样了................


推荐阅读