实例如下:
静态void testlock1(){final atomicInteger waitcount = new atomicInteger(30000);最终对象waitobj = new Object(); system.out.println(“ start”+system.currenttimemillis()); for(int i = 0; i <30000; i ++){new thread(new runnable(){@Override public void Run(){try {thread.sleep(10);} catch(InterruptedException E) } })。开始(); } while(waitcount.intvalue()> 0){同步(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(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父线程(或是主线程)等待所有子线程退出的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。,也希望大家多多支持武林网。