: :
static void testlock1 () {Final AtomicInteger WaitCount = new AtomicInteger (30000) ؛ الكائن النهائي waitobj = new Object () ؛ System.out.println ("start"+system.currentTimeMillis ()) ؛ لـ (int i = 0 ؛ i <30000 ؛ i ++) {new thread (new RunNable () {Override public void run () {try {thread.sleep (10) ؛} catch (interruptedException e) { }).يبدأ()؛ } بينما (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 ()) ؛ لـ (int i = 0 ؛ i <30000 ؛ i ++) {new thread (new RunNable () {Override public void run () {try {thread.sleep (10) ؛} catch (interruptedException e) {E.PrintStackTrace () ؛ } جرب {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 父线程 (或是主线程) 等待所有子线程退出的实例就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。