- 浏览: 467234 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
ty1972873004:
sunwang810812 写道我运行了这个例子,怎么结果是这 ...
Java并发编程: 使用Semaphore限制资源并发访问的线程数 -
lgh1992314:
simpleDean 写道请问,Logger.setLevel ...
Java内置Logger详解 -
sunwang810812:
我运行了这个例子,怎么结果是这样的:2号车泊车6号车泊车5号车 ...
Java并发编程: 使用Semaphore限制资源并发访问的线程数 -
jp260715007:
nanjiwubing123 写道参考你的用法,用如下方式实现 ...
面试题--三个线程循环打印ABC10次的几种解决方法 -
cb_0312:
SurnameDictionary文章我没看完,现在懂了
中文排序
本文将介绍CountDownLatch工具类,并采用这个工具类给出一个实例。
1. CountDownLatch工具类介绍
CountDownLatch是一个同步工具类,它允许一个或多个线程处于等待状态直到在其它线程中运行的一组操作完成为止。CountDownLatch用一个给定的计数来实现初始化。Await方法会一直处于阻塞状态,直到countDown方法调用而使当前计数达到零。当计数为零之后,所有处于等待的线程将被释放,await的任何后续调用将立即返回。这种现象只出现一次,计数是不能被重置的。如果你需要一个可以重置计数的版本,需要考虑使用CyclicBarrie.
上面的介绍来自于CountDownLatch类的注释。
/** * A synchronization aid that allows one or more threads to wait until * a set of operations being performed in other threads completes. * * <p>A {@code CountDownLatch} is initialized with a given [i]count[/i]. * The {@link #await await} methods block until the current count reaches * zero due to invocations of the {@link #countDown} method, after which * all waiting threads are released and any subsequent invocations of * {@link #await await} return immediately. This is a one-shot phenomenon * -- the count cannot be reset. If you need a version that resets the * count, consider using a {@link CyclicBarrier}. * */
CountDownLatch中定义了一个内部类Sync,该类继承AbstractQueuedSynchronizer。从代码中可以看出,CountDownLatch的await,countDown以及getCount方法都调用了Sync的方法。CountDownLatch工具类相关的类图以及详细代码如下:
/* * @(#)CountDownLatch.java 1.5 04/02/09 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util.concurrent; import java.util.concurrent.locks.*; import java.util.concurrent.atomic.*; /** * A synchronization aid that allows one or more threads to wait until * a set of operations being performed in other threads completes. * * <p>A <tt>CountDownLatch</tt> is initialized with a given * [i]count[/i]. The {@link #await await} methods block until the current * {@link #getCount count} reaches zero due to invocations of the * {@link #countDown} method, after which all waiting threads are * released and any subsequent invocations of {@link #await await} return * immediately. This is a one-shot phenomenon -- the count cannot be * reset. If you need a version that resets the count, consider using * a {@link CyclicBarrier}. * * <p>A <tt>CountDownLatch</tt> is a versatile synchronization tool * and can be used for a number of purposes. A * <tt>CountDownLatch</tt> initialized with a count of one serves as a * simple on/off latch, or gate: all threads invoking {@link #await await} * wait at the gate until it is opened by a thread invoking {@link * #countDown}. A <tt>CountDownLatch</tt> initialized to [i]N[/i] * can be used to make one thread wait until [i]N[/i] threads have * completed some action, or some action has been completed N times. * <p>A useful property of a <tt>CountDownLatch</tt> is that it * doesn't require that threads calling <tt>countDown</tt> wait for * the count to reach zero before proceeding, it simply prevents any * thread from proceeding past an {@link #await await} until all * threads could pass. * * <p><b>Sample usage:</b> Here is a pair of classes in which a group * of worker threads use two countdown latches: * [list] * <li>The first is a start signal that prevents any worker from proceeding * until the driver is ready for them to proceed; * <li>The second is a completion signal that allows the driver to wait * until all workers have completed. * [/list] * * <pre> * class Driver { // ... * void main() throws InterruptedException { * CountDownLatch startSignal = new CountDownLatch(1); * CountDownLatch doneSignal = new CountDownLatch(N); * * for (int i = 0; i < N; ++i) // create and start threads * new Thread(new Worker(startSignal, doneSignal)).start(); * * doSomethingElse(); // don't let run yet * startSignal.countDown(); // let all threads proceed * doSomethingElse(); * doneSignal.await(); // wait for all to finish * } * } * * class Worker implements Runnable { * private final CountDownLatch startSignal; * private final CountDownLatch doneSignal; * Worker(CountDownLatch startSignal, CountDownLatch doneSignal) { * this.startSignal = startSignal; * this.doneSignal = doneSignal; * } * public void run() { * try { * startSignal.await(); * doWork(); * doneSignal.countDown(); * } catch (InterruptedException ex) {} // return; * } * * void doWork() { ... } * } * * </pre> * * <p>Another typical usage would be to divide a problem into N parts, * describe each part with a Runnable that executes that portion and * counts down on the latch, and queue all the Runnables to an * Executor. When all sub-parts are complete, the coordinating thread * will be able to pass through await. (When threads must repeatedly * count down in this way, instead use a {@link CyclicBarrier}.) * * <pre> * class Driver2 { // ... * void main() throws InterruptedException { * CountDownLatch doneSignal = new CountDownLatch(N); * Executor e = ... * * for (int i = 0; i < N; ++i) // create and start threads * e.execute(new WorkerRunnable(doneSignal, i)); * * doneSignal.await(); // wait for all to finish * } * } * * class WorkerRunnable implements Runnable { * private final CountDownLatch doneSignal; * private final int i; * WorkerRunnable(CountDownLatch doneSignal, int i) { * this.doneSignal = doneSignal; * this.i = i; * } * public void run() { * try { * doWork(i); * doneSignal.countDown(); * } catch (InterruptedException ex) {} // return; * } * * void doWork() { ... } * } * * </pre> * * @since 1.5 * @author Doug Lea */ public class CountDownLatch { /** * Synchronization control For CountDownLatch. * Uses AQS state to represent count. */ private static final class Sync extends AbstractQueuedSynchronizer { Sync(int count) { setState(count); } int getCount() { return getState(); } public int tryAcquireShared(int acquires) { return getState() == 0? 1 : -1; } public boolean tryReleaseShared(int releases) { // Decrement count; signal when transition to zero for (;;) { int c = getState(); if (c == 0) return false; int nextc = c-1; if (compareAndSetState(c, nextc)) return nextc == 0; } } } private final Sync sync; /** * Constructs a <tt>CountDownLatch</tt> initialized with the given * count. * * @param count the number of times {@link #countDown} must be invoked * before threads can pass through {@link #await}. * * @throws IllegalArgumentException if <tt>count</tt> is less than zero. */ public CountDownLatch(int count) { if (count < 0) throw new IllegalArgumentException("count < 0"); this.sync = new Sync(count); } /** * Causes the current thread to wait until the latch has counted down to * zero, unless the thread is {@link Thread#interrupt interrupted}. * * <p>If the current {@link #getCount count} is zero then this method * returns immediately. * <p>If the current {@link #getCount count} is greater than zero then * the current thread becomes disabled for thread scheduling * purposes and lies dormant until one of two things happen: * [list] * <li>The count reaches zero due to invocations of the * {@link #countDown} method; or * <li>Some other thread {@link Thread#interrupt interrupts} the current * thread. * [/list] * <p>If the current thread: * [list] * <li>has its interrupted status set on entry to this method; or * <li>is {@link Thread#interrupt interrupted} while waiting, * [/list] * then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * * @throws InterruptedException if the current thread is interrupted * while waiting. */ public void await() throws InterruptedException { sync.acquireSharedInterruptibly(1); } /** * Causes the current thread to wait until the latch has counted down to * zero, unless the thread is {@link Thread#interrupt interrupted}, * or the specified waiting time elapses. * * <p>If the current {@link #getCount count} is zero then this method * returns immediately with the value <tt>true</tt>. * * <p>If the current {@link #getCount count} is greater than zero then * the current thread becomes disabled for thread scheduling * purposes and lies dormant until one of three things happen: * [list] * <li>The count reaches zero due to invocations of the * {@link #countDown} method; or * <li>Some other thread {@link Thread#interrupt interrupts} the current * thread; or * <li>The specified waiting time elapses. * [/list] * <p>If the count reaches zero then the method returns with the * value <tt>true</tt>. * <p>If the current thread: * [list] * <li>has its interrupted status set on entry to this method; or * <li>is {@link Thread#interrupt interrupted} while waiting, * [/list] * then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * * <p>If the specified waiting time elapses then the value <tt>false</tt> * is returned. * If the time is * less than or equal to zero, the method will not wait at all. * * @param timeout the maximum time to wait * @param unit the time unit of the <tt>timeout</tt> argument. * @return <tt>true</tt> if the count reached zero and <tt>false</tt> * if the waiting time elapsed before the count reached zero. * * @throws InterruptedException if the current thread is interrupted * while waiting. */ public boolean await(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } /** * Decrements the count of the latch, releasing all waiting threads if * the count reaches zero. * <p>If the current {@link #getCount count} is greater than zero then * it is decremented. If the new count is zero then all waiting threads * are re-enabled for thread scheduling purposes. * <p>If the current {@link #getCount count} equals zero then nothing * happens. */ public void countDown() { sync.releaseShared(1); } /** * Returns the current count. * <p>This method is typically used for debugging and testing purposes. * @return the current count. */ public long getCount() { return sync.getCount(); } /** * Returns a string identifying this latch, as well as its state. * The state, in brackets, includes the String * "Count =" followed by the current count. * @return a string identifying this latch, as well as its * state */ public String toString() { return super.toString() + "[Count = " + sync.getCount() + "]"; } }
2. CountDownLatch工具类的使用案例
CountDownLatch的作用是控制一个计数器,每个线程在运行完毕后执行countDown,表示自己运行结束,这对于多个子任务的计算特别有效,比如一个异步任务需要拆分成10个子任务执行,主任务必须知道子任务是否完成,所有子任务完成后才能进行合并计算,从而保证了一个主任务逻辑的正确性。(此段摘自于<<改善Java程序的151个建议>>, P254)
CountDownLatch最重要的方法是countDown()和await(),前者主要是倒数一次,后者是等待倒数到0,如果没有到达0,就只有阻塞等待了。
本实例主要使用CountDownLatch工具类来实现10个线程对1~100的求和,每个线程对10个数进行求和。第一个线程对1 – 10求和
第二个线程对 11 – 20求和
第三个线程对21 – 30 求和
…..
第十个线程对91 – 100求和。
具体的代码如下:
package my.concurrent.countdown; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; public class Calculator implements Callable<Integer> { //开始信号 private final CountDownLatch startSignal; //结束信号 private final CountDownLatch doneSignal; private int groupNumber = 0; /** * @param startSignal * @param endSignal * @param groupId */ public Calculator(CountDownLatch startSignal, CountDownLatch doneSignal, int groupNumber) { this.startSignal = startSignal; this.doneSignal = doneSignal; this.groupNumber = groupNumber; } public Integer call() throws Exception { startSignal.await(); Integer result = sum(groupNumber); printCompleteInfor(groupNumber,result); doneSignal.countDown(); return result; } private Integer sum(int groupNumber) { if (groupNumber < 1) { throw new IllegalArgumentException(); } int sum = 0; int start = (groupNumber - 1) * 10 + 1; int end = groupNumber * 10; for (int i = start; i <= end; i++) { sum += i; } return sum; } private void printCompleteInfor(int groupNumber, int sum) { System.out.println(String.format("Group %d is finished, the sum in this gropu is %d", groupNumber, sum)); } }
package my.concurrent.countdown; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class CountDownLatchTest { public static void main(String[] args) throws Exception { /** * 1-100求和,分10个线程来计算,每个线程对10个数求和。 */ int numOfGroups = 10; CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(numOfGroups); ExecutorService service = Executors.newFixedThreadPool(numOfGroups); List<Future<Integer>> futures = new ArrayList<Future<Integer>>(); submit(futures, numOfGroups, service, startSignal, doneSignal); /** * 开始,让所有的求和计算线程运行 */ startSignal.countDown(); /** * 阻塞,知道所有计算线程完成计算 */ doneSignal.await(); shutdown(service); printResult(futures); } private static void submit(List<Future<Integer>> futures, int numOfGroups, ExecutorService service, CountDownLatch startSignal, CountDownLatch doneSignal) { for (int groupNumber = 1; groupNumber <= numOfGroups; groupNumber++) { futures.add(service.submit(new Calculator(startSignal, doneSignal, groupNumber))); } } private static int getResult(List<Future<Integer>> futures) throws InterruptedException, ExecutionException { int result = 0; for (Future<Integer> f : futures) { result += f.get(); } return result; } private static void printResult(List<Future<Integer>> futures) throws InterruptedException, ExecutionException { System.out.println("[1,100] Sum is :" + getResult(futures)); } private static void shutdown(ExecutorService service) { service.shutdown(); } }
一次的执行结果如下:
Group 8 is finished, the sum in this gropu is 755
Group 2 is finished, the sum in this gropu is 155
Group 10 is finished, the sum in this gropu is 955
Group 5 is finished, the sum in this gropu is 455
Group 7 is finished, the sum in this gropu is 655
Group 3 is finished, the sum in this gropu is 255
Group 9 is finished, the sum in this gropu is 855
Group 1 is finished, the sum in this gropu is 55
Group 4 is finished, the sum in this gropu is 355
Group 6 is finished, the sum in this gropu is 555
[1,100] Sum is :5050
评论
8 楼
julydave
2013-08-05
MouseLearnJava 写道
freezingsky 写道
内容基本与《java并发编程实践》很相似。不过,在这文章里如果能增加CyclicBarrier的内容,并做出相应的比较,会更好。
谢谢你的建议。 我会在下一篇介绍CuclicBarrier内容的时候,做出一些比较的。
可参见此篇文章 http://www.molotang.com/articles/487.html
7 楼
julydave
2013-08-05
freezingsky 写道
内容基本与《java并发编程实践》很相似。不过,在这文章里如果能增加CyclicBarrier的内容,并做出相应的比较,会更好。
可以看看这篇 http://www.molotang.com/articles/487.html
6 楼
MouseLearnJava
2013-07-31
freezingsky 写道
内容基本与《java并发编程实践》很相似。不过,在这文章里如果能增加CyclicBarrier的内容,并做出相应的比较,会更好。
谢谢你的建议。 我会在下一篇介绍CuclicBarrier内容的时候,做出一些比较的。
5 楼
freezingsky
2013-07-30
内容基本与《java并发编程实践》很相似。不过,在这文章里如果能增加CyclicBarrier的内容,并做出相应的比较,会更好。
4 楼
MouseLearnJava
2013-07-30
/** * 开始,让所有的求和计算线程运行 */ startSignal.countDown(); /** * 阻塞,直到所有计算线程完成计算 */ doneSignal.await(); shutdown(service); /** * 计算并打印出求和内容 */ printResult(futures);
doneSignal.await();是用于阻塞,直到所有子线程完成计算。
在这之后,主线程才去求和,本文的代码中可以看出,计算和打印结果放在最后了,这时候,所有的线程都有了计算结果。
3 楼
fisher123
2013-07-30
doneSignal.await(); 不也是阻塞线程直到所有线程执行完毕么?
2 楼
MouseLearnJava
2013-07-30
文中所述的方法是采用CountDownLatch工具类来做的,该例子中,1-100的求和分十个子线程来做,主线程是在所有线程计算好结果之后,才把所有子线程得到的结果累加起来,主线程求和过程中不会有线程等待的情况。
如果直接采用future.get()方法来做,比如一个for循环中采用sum+=future.get()去求和, 可能会有等待的现象,因为多线程下执行的顺序是不定的。比如:可能先执行2,4,5,再执行1,那么尝试用future.get()取第一个线程产生的结果的时候,第一个线程的结果可能还没有计算好,这样就会产生等待的情况了。
Future接口的get方法如下:
如果直接采用future.get()方法来做,比如一个for循环中采用sum+=future.get()去求和, 可能会有等待的现象,因为多线程下执行的顺序是不定的。比如:可能先执行2,4,5,再执行1,那么尝试用future.get()取第一个线程产生的结果的时候,第一个线程的结果可能还没有计算好,这样就会产生等待的情况了。
Future接口的get方法如下:
/** * Waits if necessary for the computation to complete, and then * retrieves its result. * * @return the computed result * @throws CancellationException if the computation was cancelled * @throws ExecutionException if the computation threw an * exception * @throws InterruptedException if the current thread was interrupted * while waiting */ V get() throws InterruptedException, ExecutionException; /** * Waits if necessary for at most the given time for the computation * to complete, and then retrieves its result, if available. * * @param timeout the maximum time to wait * @param unit the time unit of the timeout argument * @return the computed result * @throws CancellationException if the computation was cancelled * @throws ExecutionException if the computation threw an * exception * @throws InterruptedException if the current thread was interrupted * while waiting * @throws TimeoutException if the wait timed out */ V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
1 楼
fisher123
2013-07-30
请问一下LZ,文中所述方法和直接调用future.get(),有何不同之处呢?求教
发表评论
-
Java编程练手100题
2014-12-11 17:13 6734本文给出100道Java编程练手的程序。 列表如下: 面 ... -
The Code Sample 代码实例 个人博客开通
2014-09-04 18:48 1423个人博客小站开通 http://thecodesample. ... -
三道多线程编程题
2013-10-14 14:30 5923本文记录三道多线程编程题。 第一道: 有四个线程1、2、3 ... -
面试题 -- 一个线程打印 1~52,另一个线程打印字母A-Z。打印顺序为12A34B56C……5152Z。
2013-10-01 13:03 5039题目:一个线程打印 1~52,另一个线程打印字母A-Z。打印顺 ... -
面试题--三个线程循环打印ABC10次的几种解决方法
2013-09-29 20:01 10343最近发现公司有份笔试试卷中有道多线程的题目:有三个线程分别打印 ... -
Java并发编程-Condition使用实例
2013-09-27 21:35 4897本文将简单介绍用于线程协作的Condition, 并给出一个例 ... -
Java并发编程: 使用Exchanger实现线程间的数据交换
2013-08-08 12:13 3772本文介绍Exchanger工具类, 然后采用Exchanger ... -
Java并发编程: 使用Semaphore限制资源并发访问的线程数
2013-08-07 16:23 4766本文将介绍用来控制资源同时访问个数的Semaphore工具类, ... -
Java并发编程: Volatile不能保证数据同步
2013-08-05 12:47 6038在本篇博文中,将给出一 ... -
Java并发编程: CyclicBarrier让多线程齐步走
2013-08-01 11:12 4552以前在<<编写高质量代码-改善Java程序的151 ... -
Java并发编程-Executor框架+实例
2013-07-18 21:23 3838Executor框架是指java 5中 ...
相关推荐
《Java并发编程:设计原则与模式(第二版)》是一本深入探讨Java多线程编程技术的权威著作。这本书详细阐述了在Java平台中进行高效并发处理的关键概念、设计原则和实用模式。以下是对该书内容的一些核心知识点的概述...
《Java并发编程:设计原则与模式》是一本深入探讨Java多线程编程的书籍,它涵盖了并发编程中的关键概念、原则和模式。在Java中,并发处理是优化应用程序性能、提高资源利用率的重要手段,尤其在现代多核处理器的环境...
《Java并发编程:设计原则与模式(第二版)》是一本深入探讨Java平台上的多线程和并发编程的权威著作。这本书旨在帮助开发者理解和掌握如何有效地编写可扩展且高效的并发程序。以下是书中涵盖的一些关键知识点: 1....
总的来说,“Java并发编程:设计原则与模式”涵盖了从基础到高级的并发编程概念,帮助开发者理解和应用Java平台的并发特性,以构建可扩展、高性能的多线程应用程序。通过深入学习和实践这些知识点,开发者可以在...
《Java并发编程 设计原则与模式 第二版》是一本深受程序员喜爱的经典书籍,由Addison Wesley出版。这本书深入探讨了Java平台上的多线程编程技术,为开发者提供了丰富的设计原则和模式,帮助他们理解和解决并发环境中...
《Java并发编程实战》是Java并发编程领域的一本经典著作,它深入浅出地介绍了如何在Java平台上进行高效的多线程编程。这本书的源码提供了丰富的示例,可以帮助读者更好地理解书中的理论知识并将其应用到实际项目中。...
《Java并发编程:设计原则与模式(第二版)》是一本深入探讨Java多线程编程技术的权威著作。这本书详细阐述了如何在Java环境中高效、安全地进行并发编程,涵盖了多线程设计的关键原则和常见模式。对于Java开发者来说...
根据提供的信息,“Java 并发编程实战.pdf”这本书聚焦于Java并发编程的实践与应用,旨在帮助读者深入了解并掌握Java中的多线程技术及其在实际项目中的应用技巧。虽然部分内容未能提供具体章节或实例,但从标题及...
本文将深入探讨Java并发编程的设计原则与模式,旨在帮助开发者理解并有效地应用这些原则和模式。 一、并发编程的基础概念 并发是指两个或多个操作在同一时间间隔内执行,而不是严格意义上的同一时刻。在Java中,...
Java提供了丰富的并发工具和API,包括线程、锁、同步、并发集合、并发工具类以及Java内存模型(JMM),这些都是Java并发编程的基础。 1. **线程与进程**:在Java中,线程是程序执行的基本单元,而进程是系统分配...
Java并发编程是软件开发中的重要领域,特别是在多核处理器和分布式系统中,高效地利用并发可以极大地提升程序的性能和响应速度。本资源"Java并发编程_设计原则和模式(CHM)"聚焦于Java语言在并发环境下的编程技巧、...
以上知识点覆盖了Java并发编程的主要方面,包括线程管理、同步机制、并发工具、设计模式、并发集合以及并发编程的最佳实践等,是理解和掌握Java并发编程的关键。在实际开发中,理解和熟练运用这些知识可以编写出高效...
通过深入学习《JAVA并发编程艺术》,开发者能更好地理解并发编程的原理,熟练运用Java提供的并发工具和API,解决实际开发中的多线程问题,提高软件的性能和稳定性。这是一本值得每一位Java开发者研读的书。
Java并发编程是Java开发中的重要领域,特别是在多核处理器和分布式系统中,高效地利用并发可以极大地提升程序的性能和响应速度。《java并发编程的核心方法和框架》这本书旨在深入探讨这一主题,帮助开发者掌握Java...
在Java并发编程中,多线程是核心概念之一。多线程允许程序同时执行多个任务,从而充分利用系统资源,提高程序性能。然而,多线程编程也带来了同步和竞态条件等问题,这需要开发者具备良好的线程管理和同步机制的知识...
《Java并发编程实践》是一本深入探讨Java多线程编程的经典著作,由Brian Goetz、Tim Peierls、Joshua Bloch、Joseph Bowles和David Holmes等专家共同编写。这本书全面介绍了Java平台上的并发编程技术,是Java开发...
Java并发编程中的`JUC`(Java Util Concurrency)库是Java平台中用于处理多线程问题的核心工具包,它提供了一系列高效、线程安全的工具类,帮助开发者编写并发应用程序。`AQS`(AbstractQueuedSynchronizer)是JUC库中的...
"Java并发编程与实践"文档深入剖析了这一主题,旨在帮助开发者理解和掌握如何在Java环境中有效地实现并发。 并发是指在单个执行单元(如CPU)中同时执行两个或更多任务的能力。在Java中,这主要通过线程来实现,...
总而言之,《Java并发编程艺术》这本书将系统性地介绍Java并发编程的各种技术和最佳实践,帮助读者提升在多线程环境下的编程能力,从而设计出更加健壮、高效的Java应用。无论你是初级开发者还是经验丰富的工程师,这...