java并发编程同步器countdownlatch-mile米乐体育
countdownlatch
在日常开发中经常会遇到需要在主线程中开启多个线程去并行执行任务,并且主线程需要等待所有子线程执行完毕后再进行汇总的场景。在 countdownlatch 出现之前般都使用线程的join()方法来实现这一点,但是 join 方法不够灵活,不能够满足不同场景的需要,所以 jdk 开发组提供了 countdownlatch
这个类,我们前面介绍的例子使用 coumtdownlatch
会更优雅。
使用countdownlatch 的代码如下:
package locksupporttest; import java.util.concurrent.countdownlatch; public class joincountdownlatch { private static volatile countdownlatch countdownlatch = new countdownlatch(2); public static void main(string[] args) throws interruptedexception{ thread threadone = new thread(new runnable() { @override public void run() { try { thread.sleep(1000); system.out.println("child threadone over!"); } catch (interruptedexception e) { e.printstacktrace(); } finally { countdownlatch.countdown(); } } }); thread threadtwo = new thread(new runnable() { @override public void run() { try { thread.sleep(1000); system.out.println("child threadone over!"); } catch (interruptedexception e) { e.printstacktrace(); } finally { countdownlatch.countdown(); } } }); threadone.start(); threadtwo.start(); system.out.println("wait all child thread over!!!"); countdownlatch.await(); system.out.println("all child thread over!"); } }
在如上代码中,创建了一个 countdownlatch
实例,因为有两个子线程所以构造函数的传参为2。主线程调用countdownlatch.await()
方法后会被阻塞。子线程执行完毕后调用 countdownlatch.countdown()
方法让 countdownlatch 内部的计数器减1,所有子线程执行完毕并调用 countdown()方法后计数器会变为0,这时候主线程的await()方法才会返回。其实上面的代码还不够优雅,在项目实践中一般都避免直接操作线程,而是使用 exceutorservice线程池来管理,使用excsuiwsnise时传递的参数是 runable 或者 callable对象,这时候你没有办法直接调用这些线程的join()方法,这就需要选择使用countdownlatch
了。
将上面的代码修改为:
package locksupporttest; import java.util.concurr恰卡编程网ent.countdownlatch; import java.util.concurrent.executor; import java.util.concurrent.executorservice; import java.util.concurrent.executors; public class joincountdownlatch2 { private static volatile countdownlatch countdownlatch = new countdownlatch(2); public static void main(string[] args) throws interruptedexception{ executorservice executorservice = executors.newfixedthreadpool(2); executorservice.submit(new runnable() { @override public void run() { try { thread.sleep(1000); system.out.println("child threadone over!"); } catch (interruptedexception e) { e.printstacktrace(); } finally { countdownlatch.countdown(); } } }); executorservice.submit(new runnable() { @override public void rwww.cppcns.comun() { try { thread.sleep(1000); system.out.println("child threadtwo over!"); } catch (interruptedexception e) { e.printstacktrace(); } finally { countdownlatch.countdown(); } } }); system.out.println("wait all child thread over!!!"); countdownlatch.await(); system.out.println("all child thread over!"); executorservice.shutdown(); } }
最后总结一下countdownlatch与join()的区别。一个区别是,调用一个子线程的join()方法后,该线程会一直被阻塞直到子线程运行完毕,而 countdownlatch 则使用计数器来允许子线程运行完毕或者在运行中递减计数,也就是 countdownlach 可以在子线程运行的任何时候让 await() 方法返回而不一定必须等到线程结東。另外,使用线程池来管理线程时一般都是直接添加 runable 到线程池,这时候就没有办法再调用线程的 join 方法了,就是说 coundownlatch 相比 join 方法让我们对线程同步有更灵活的控制。
到此这篇关于java并发编程同步器countdownlatch的文章就介绍到这了,更多相关java countdownlatch内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!