实例如下 :
정적 void testlock1 () {Final atomicinteger WaitCount = new atomicinteger (30000); 최종 개체 waitobj = new Object (); System.out.println ( "시작"+System.CurrentTimeMillis ()); for (int i = 0; i <30000; i ++) {new 스레드 (new Runnable () {@override public void run () {try {thread.sleep (10);} catch (InterpruptedException e) {e.printstacktrace ();} WaitCount.decrementAndget (synchronized ();}; }).시작(); } while (waitCount.intValue ()> 0) {synchronized (waitobj) {if (waitCount.IntValue ()> 0) {try {waitobj.wait (); } catch (InterruptedException e) {e.printstacktrace (); }}}}} system.out.println ( "OK"+System.CurrentTimeMillis ()); } static void testlock2 () {Final CountdownLatch Worklauch = new CountdownLatch (30000); // 计数器 计数器 计数器 计数器 计数器 计数器 计数器 计数器 计数器 计数器 计数器 计数器 system.out.println ( "start2"+system.currenttimeMillis ()); for (int i = 0; i <30000; i ++) {new Thread (new Runnable () {@override public void run () {try {thread.sleep (10);} catch (InterruptedException e) {e.printstacktrace ();} worklauch.countdown ();}}; } try {worklauch.await (); } catch (InterruptedException e) {e.printstacktrace (); } system.out.println ( "ok2"+system.currenttimeMillis ()); } public static void main (String [] args) {testlock1 (); testlock2 (); }第一种是我随便写的实现, 有点糙。第二种是朋友告知的一个类 有点糙。第二种是朋友告知的一个类, java 的 동시 中的 中的, 据说还有几个相似功能的类实现。这 30000 个线程 时间差大概是不到 200ms 的样子
以上这篇 Java a (或是主线程 或是主线程) 等待所有子线程退出的实例就是小编分享给大家的全部内容了 等待所有子线程退出的实例就是小编分享给大家的全部内容了, 希望能给大家一个参考, 也希望大家多多支持武林网。