- 浏览: 984946 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
Java 理论与实践: 正确使用 Volatile 变量:http://www.ibm.com/developerworks/cn/java/j-jtp06197.html
聊聊并发(一)——深入分析Volatile的实现原理:http://www.infoq.com/cn/articles/ftf-java-volatile
深入理解Java内存模型(四)——volatile:http://www.infoq.com/cn/articles/java-memory-model-4/
Java的多线程机制系列:(四)不得不提的volatile及指令重排序(happen-before):
http://www.cnblogs.com/mengheng/p/3495379.html
CountDownLatch同步化的一个辅助工具,允许一个或多个线程等待,直到所有线程中执行完成
比如主线程计算一个复杂的计算表达式,将表达式分为多个子表达式在线程中去计算,
主线程要计算表达式的最后值,必须等所有的线程计算完子表达式计算,方可计算表达式的值;
再比如一个团队赛跑游戏,最后要计算团队赛跑的成绩,主线程计算最后成绩,要等到所有
团队成员跑完,方可计算总成绩。使用情况两种:第一种,所有线程等待一个开始信息号,当开始信息号启动时,所有线程执行,等待所有线程执行完;第二种,所有线程放在线程池中,执行,等待所有线程执行完,方可执行主线程任务方可执行主线程任务。
测试有统一开始信号的情况:
测试结果:
开始赛跑......
选手3正在赛跑中........
选手2正在赛跑中........
选手1正在赛跑中........
团队赛跑结束,最后成绩为:6000
测试第二种情况,没有开始信号
测试结果:
售票员1卖完一张票,还有:8张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:9张
售票员3卖完一张票,还有:5张
售票员1卖完一张票,还有:6张
售票员3卖完一张票,还有:3张
售票员2卖完一张票,还有:4张
售票员3卖完一张票,还有:1张
售票员1卖完一张票,还有:2张
售票员2卖完一张票,还有:0张
票已售完,所有售票员,停止售票
有时测试结果为:
售票员1卖完一张票,还有:9张
售票员2卖完一张票,还有:8张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:6张
售票员1卖完一张票,还有:5张
售票员3卖完一张票,还有:4张
售票员3卖完一张票,还有:2张
售票员2卖完一张票,还有:1张
售票员1卖完一张票,还有:3张
售票员1卖完一张票,还有:0张
售票员2卖完一张票,还有:-1张
票已售完,所有售票员,停止售票
把Thread.sleep(1000)这句去掉,票数为负数的概率较小,但还是会出现,
我测试了3分钟,出现2次。
关注这一句:
while(tickets.get()>0){
问题可能在tickets.get()的时候,票数不为零,在卖票的时候,其他线程已经在其基础上,修改
tickets,而当前线程tickets.get()已经做过判断,不为零,导致的错误。
再次修改:
测试:
售票员1卖完一张票,还有:9张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:8张
售票员3卖完一张票,还有:5张
售票员1卖完一张票,还有:6张
售票员3卖完一张票,还有:3张
售票员2卖完一张票,还有:4张
售票员2卖完一张票,还有:0张
售票员3卖完一张票,还有:1张
售票员1卖完一张票,还有:2张
票已售完,所有售票员,停止售票
不在出现上述票数为负的情况,关键是添加了所有线程都可见,且线程安全的余票状态
private static volatile AtomicBoolean isDoned = new AtomicBoolean(Boolean.TRUE);
下面来看使用同步锁的场景:
控制台输出:
售票员1卖完一张票,还有:9张
售票员1卖完一张票,还有:8张
售票员1卖完一张票,还有:7张
售票员1卖完一张票,还有:6张
售票员1卖完一张票,还有:5张
售票员1卖完一张票,还有:4张
售票员1卖完一张票,还有:3张
售票员1卖完一张票,还有:2张
售票员3卖完一张票,还有:1张
售票员3卖完一张票,还有:0张
票已售完,所有售票员,停止售票
虽然也可以控制卖票,但只有两个售票员在售票,不能有效的利用资源,达到最大性能。
从上面可以看出,最有效方式为第二种。
聊聊并发(一)——深入分析Volatile的实现原理:http://www.infoq.com/cn/articles/ftf-java-volatile
深入理解Java内存模型(四)——volatile:http://www.infoq.com/cn/articles/java-memory-model-4/
Java的多线程机制系列:(四)不得不提的volatile及指令重排序(happen-before):
http://www.cnblogs.com/mengheng/p/3495379.html
CountDownLatch同步化的一个辅助工具,允许一个或多个线程等待,直到所有线程中执行完成
比如主线程计算一个复杂的计算表达式,将表达式分为多个子表达式在线程中去计算,
主线程要计算表达式的最后值,必须等所有的线程计算完子表达式计算,方可计算表达式的值;
再比如一个团队赛跑游戏,最后要计算团队赛跑的成绩,主线程计算最后成绩,要等到所有
团队成员跑完,方可计算总成绩。使用情况两种:第一种,所有线程等待一个开始信息号,当开始信息号启动时,所有线程执行,等待所有线程执行完;第二种,所有线程放在线程池中,执行,等待所有线程执行完,方可执行主线程任务方可执行主线程任务。
测试有统一开始信号的情况:
package juc; 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 RunnerGames { public static void main(String[] args) { CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new CountDownLatch(3); ExecutorService exec = Executors.newCachedThreadPool(); RunnableMan rm1 = new RunnableMan(startSignal,doneSignal, 1000); RunnableMan rm2 = new RunnableMan(startSignal,doneSignal, 2000); RunnableMan rm3 = new RunnableMan(startSignal,doneSignal, 3000); Future<Integer> score1 = exec.submit(rm1); Future<Integer> score2 = exec.submit(rm2); Future<Integer> score3 = exec.submit(rm3); System.out.println("开始赛跑......"); startSignal.countDown(); try { doneSignal.await(); } catch (InterruptedException e1) { e1.printStackTrace(); } int sumScores =0; try { try { sumScores = score1.get()+score2.get()+score3.get(); } catch (InterruptedException e) { e.printStackTrace(); } } catch (ExecutionException e) { e.printStackTrace(); } System.out.println("团队赛跑结束,最后成绩为:"+sumScores); exec.shutdown(); } }
package juc; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; class RunnableMan implements Callable<Integer> { private final CountDownLatch startSignal; private final CountDownLatch doneSignal; private final int i; RunnableMan(CountDownLatch startSignal,CountDownLatch doneSignal, int i) { this.startSignal = startSignal; this.doneSignal = doneSignal; this.i = i; } public Integer call() { try { startSignal.await(); doRun(i); } catch (InterruptedException e) { e.printStackTrace(); } doneSignal.countDown(); return new Integer(i); } void doRun(int i) throws InterruptedException { System.out.println("选手"+i/1000+"正在赛跑中........"); Thread.sleep(i*2); } }
测试结果:
开始赛跑......
选手3正在赛跑中........
选手2正在赛跑中........
选手1正在赛跑中........
团队赛跑结束,最后成绩为:6000
测试第二种情况,没有开始信号
package juc; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; /**AtomicInteger,一个提供原子操作的Integer的类。 在Java语言中,++i和i++操作并不是线程安全的,在使用的时候, 不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。*/ public class ShopTickets { private static volatile AtomicInteger tickets = new AtomicInteger(10); public static void main(String[] args) throws InterruptedException { CountDownLatch doneSignal = new CountDownLatch(3); Executor exec = Executors.newCachedThreadPool(); exec.execute(new TicketSales(doneSignal,"售票员1",tickets)); exec.execute(new TicketSales(doneSignal,"售票员2",tickets)); exec.execute(new TicketSales(doneSignal,"售票员3",tickets)); doneSignal.await(); System.out.println("票已售完,所有售票员,停止售票"); } }
package juc; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; class TicketSales implements Runnable { private final CountDownLatch doneSignal; private final AtomicInteger tickets; private String saleName; TicketSales(CountDownLatch doneSignal, String saleName,AtomicInteger tickets) { this.doneSignal = doneSignal; this.saleName = saleName; this.tickets = tickets; } public void run() { doSales(tickets); } public void doSales(AtomicInteger tickets) { try { while(tickets.get()>0){ System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张"); Thread.sleep(1000); } doneSignal.countDown(); }catch (InterruptedException e) { e.printStackTrace(); } } }
测试结果:
售票员1卖完一张票,还有:8张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:9张
售票员3卖完一张票,还有:5张
售票员1卖完一张票,还有:6张
售票员3卖完一张票,还有:3张
售票员2卖完一张票,还有:4张
售票员3卖完一张票,还有:1张
售票员1卖完一张票,还有:2张
售票员2卖完一张票,还有:0张
票已售完,所有售票员,停止售票
有时测试结果为:
售票员1卖完一张票,还有:9张
售票员2卖完一张票,还有:8张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:6张
售票员1卖完一张票,还有:5张
售票员3卖完一张票,还有:4张
售票员3卖完一张票,还有:2张
售票员2卖完一张票,还有:1张
售票员1卖完一张票,还有:3张
售票员1卖完一张票,还有:0张
售票员2卖完一张票,还有:-1张
票已售完,所有售票员,停止售票
把Thread.sleep(1000)这句去掉,票数为负数的概率较小,但还是会出现,
我测试了3分钟,出现2次。
关注这一句:
while(tickets.get()>0){
问题可能在tickets.get()的时候,票数不为零,在卖票的时候,其他线程已经在其基础上,修改
tickets,而当前线程tickets.get()已经做过判断,不为零,导致的错误。
再次修改:
package juc; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public class ShopTickets2 { /** * AtomicInteger,一个提供原子操作的Integer的类。 在Java语言中,++i和i++操作并不是线程安全的,在使用的时候, * 不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。 */ private static volatile AtomicInteger tickets = new AtomicInteger(10); public static void main(String[] args) throws InterruptedException { CountDownLatch doneSignal = new CountDownLatch(3); Executor exec = Executors.newCachedThreadPool(); exec.execute(new TicketSales2(doneSignal, "售票员1", tickets)); exec.execute(new TicketSales2(doneSignal, "售票员2", tickets)); exec.execute(new TicketSales2(doneSignal, "售票员3", tickets)); doneSignal.await(); System.out.println("票已售完,所有售票员,停止售票"); } }
package juc; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; class TicketSales2 implements Runnable { private static volatile AtomicBoolean isDoned = new AtomicBoolean(Boolean.TRUE); private final CountDownLatch doneSignal; private final AtomicInteger tickets; private String saleName; TicketSales2(CountDownLatch doneSignal, String saleName,AtomicInteger tickets) { this.doneSignal = doneSignal; this.saleName = saleName; this.tickets = tickets; } public void run() { doSales(tickets); } public void doSales(AtomicInteger tickets) { while(isDoned.get()){ System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张"); if(tickets.get()==0){ isDoned.compareAndSet(true, false); } } doneSignal.countDown(); } }
测试:
售票员1卖完一张票,还有:9张
售票员3卖完一张票,还有:7张
售票员2卖完一张票,还有:8张
售票员3卖完一张票,还有:5张
售票员1卖完一张票,还有:6张
售票员3卖完一张票,还有:3张
售票员2卖完一张票,还有:4张
售票员2卖完一张票,还有:0张
售票员3卖完一张票,还有:1张
售票员1卖完一张票,还有:2张
票已售完,所有售票员,停止售票
不在出现上述票数为负的情况,关键是添加了所有线程都可见,且线程安全的余票状态
private static volatile AtomicBoolean isDoned = new AtomicBoolean(Boolean.TRUE);
下面来看使用同步锁的场景:
package juc; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; public class ShopTickets3 { /** * AtomicInteger,一个提供原子操作的Integer的类。 在Java语言中,++i和i++操作并不是线程安全的,在使用的时候, * 不可避免的会用到synchronized关键字。而AtomicInteger则通过一种线程安全的加减操作接口。 */ private static volatile AtomicInteger tickets = new AtomicInteger(10); private static volatile Object saleLock = new Object(); public static void main(String[] args) throws InterruptedException { CountDownLatch doneSignal = new CountDownLatch(3); Executor exec = Executors.newCachedThreadPool(); exec.execute(new TicketSales3(doneSignal, "售票员1", tickets,saleLock)); exec.execute(new TicketSales3(doneSignal, "售票员2", tickets,saleLock)); exec.execute(new TicketSales3(doneSignal, "售票员3", tickets,saleLock)); doneSignal.await(); System.out.println("票已售完,所有售票员,停止售票"); } }
package juc; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; class TicketSales3 implements Runnable { private final CountDownLatch doneSignal; private final AtomicInteger tickets; private final Object saleLock; private String saleName; TicketSales3(CountDownLatch doneSignal, String saleName,AtomicInteger tickets, Object saleLock) { this.doneSignal = doneSignal; this.saleName = saleName; this.tickets = tickets; this.saleLock = saleLock; } public void run() { doSales(tickets); } public void doSales(AtomicInteger tickets) { boolean flag = true; while(flag){ synchronized(saleLock){ if(tickets.get()>0){ System.out.println(saleName+"卖完一张票,还有:"+tickets.decrementAndGet()+"张"); saleLock.notifyAll(); } else{ flag= false; } } } doneSignal.countDown(); } }
控制台输出:
售票员1卖完一张票,还有:9张
售票员1卖完一张票,还有:8张
售票员1卖完一张票,还有:7张
售票员1卖完一张票,还有:6张
售票员1卖完一张票,还有:5张
售票员1卖完一张票,还有:4张
售票员1卖完一张票,还有:3张
售票员1卖完一张票,还有:2张
售票员3卖完一张票,还有:1张
售票员3卖完一张票,还有:0张
票已售完,所有售票员,停止售票
虽然也可以控制卖票,但只有两个售票员在售票,不能有效的利用资源,达到最大性能。
从上面可以看出,最有效方式为第二种。
/* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ 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 {@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有一个初始值,调用await的线程,要等待CountDownLatch值为0时,方可 *往下执行 * <p>A {@code CountDownLatch} is a versatile synchronization tool * and can be used for a number of purposes. A * {@code CountDownLatch} 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 {@code CountDownLatch} 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 {@code CountDownLatch} is that it * doesn't require that threads calling {@code countDown} 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> * * <p>Memory consistency effects: Until the count reaches * zero, actions in a thread prior to calling * {@code countDown()} * [url=package-summary.html#MemoryVisibility]<i>happen-before</i>[/url] * actions following a successful return from a corresponding * {@code await()} in another thread. * * @since 1.5 * @author Doug Lea */ public class CountDownLatch { /** * Synchronization control For CountDownLatch. * Uses AQS state to represent count. *用一个 AQS状态代表信号量数 */ private static final class Sync extends AbstractQueuedSynchronizer { private static final long serialVersionUID = 4982264981922014374L; Sync(int count) { setState(count); } int getCount() { return getState(); } //获取信号量 protected int tryAcquireShared(int acquires) { return (getState() == 0) ? 1 : -1; } //释放信号量 protected 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 {@code CountDownLatch} 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 {@code count} is negative */ 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 {@linkplain Thread#interrupt interrupted}. * * <p>If the current count is zero then this method returns immediately. * * <p>If the current 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 {@linkplain 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 {@linkplain 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 {@linkplain Thread#interrupt interrupted}, * or the specified waiting time elapses. * * <p>If the current count is zero then this method returns immediately * with the value {@code true}. * * <p>If the current 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 {@linkplain 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 {@code true}. * * <p>If the current thread: * [list] * <li>has its interrupted status set on entry to this method; or * <li>is {@linkplain 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 {@code false} * 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 {@code timeout} argument * @return {@code true} if the count reached zero and {@code false} * 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 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 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 {@code "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() + "]"; } }
发表评论
-
Executors解析
2017-04-07 14:38 1256ThreadPoolExecutor解析一(核心线程池数量、线 ... -
ScheduledThreadPoolExecutor解析三(关闭线程池)
2017-04-06 20:52 4457ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析二(任务调度)
2017-04-06 12:56 2127ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析一(调度任务,任务队列)
2017-04-04 22:59 4992Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析四(线程池关闭)
2017-04-03 23:02 9113Executor接口的定义:http: ... -
ThreadPoolExecutor解析三(线程池执行提交任务)
2017-04-03 12:06 6088Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析二(线程工厂、工作线程,拒绝策略等)
2017-04-01 17:12 3041Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析一(核心线程池数量、线程池状态等)
2017-03-31 22:01 20521Executor接口的定义:http://donald-dra ... -
ScheduledExecutorService接口定义
2017-03-29 12:53 1511Executor接口的定义:http://donald-dra ... -
AbstractExecutorService解析
2017-03-29 08:27 1081Executor接口的定义:http: ... -
ExecutorCompletionService解析
2017-03-28 14:27 1596Executor接口的定义:http://donald-dra ... -
CompletionService接口定义
2017-03-28 12:39 1069Executor接口的定义:http://donald-dra ... -
FutureTask解析
2017-03-27 12:59 1330package java.util.concurrent; ... -
Future接口定义
2017-03-26 09:40 1198/* * Written by Doug Lea with ... -
ExecutorService接口定义
2017-03-25 22:14 1164Executor接口的定义:http://donald-dra ... -
Executor接口的定义
2017-03-24 23:24 1678package java.util.concurrent; ... -
简单测试线程池拒绝执行任务策略
2017-03-24 22:37 2031线程池多余任务的拒绝执行策略有四中,分别是直接丢弃任务Disc ... -
JAVA集合类简单综述
2017-03-23 22:51 925Queue接口定义:http://donald-draper. ... -
DelayQueue解析
2017-03-23 11:00 1737Queue接口定义:http://donald-draper. ... -
SynchronousQueue解析下-TransferQueue
2017-03-22 22:20 2140Queue接口定义:http://donald-draper. ...
相关推荐
于是乎到现在的Hibernate、MyBatis、Spring、Spring MVC、AQS、ThreadPoolExecutor、CountDownLatch使用场景和核心源码分析。 感觉自己还是真的菜鸡,有太多框架的底层实现都不怎么了解。 当山头被一座一座攻克时,...
本文将对CountDownLatch和CyclicBarrier的使用场景和实现进行详细的介绍。 CountDownLatch的应用场景: CountDownLatch是一个非常有用的线程控制工具,它可以使一个线程等待其他线程达到某一目标后进行自己的下...
本文将深入探讨CountDownLatch的工作原理、使用场景以及相关源码分析。 CountDownLatch是一个计数器,初始化时设定一个初始值,通常表示一个任务的子任务数量。每个线程完成其工作后会调用`countDown()`方法,...
JAVA多线程CountDownLatch使用详解 JAVA多线程CountDownLatch是JAVA多线程编程中的一种同步工具,主要用来让某个线程等待其他线程执行完毕后再继续执行。下面我们将详细介绍JAVA多线程CountDownLatch的使用和原理。...
CountDownLatch常用于启动多线程后等待所有线程完成后再进行下一步,而CyclicBarrier则适用于多线程协同工作,每个阶段完成后所有线程再次聚合,比如赛车游戏的起点等待、分布式计算中的数据分区处理等场景。...
总的来说,`CountDownLatch` 是一种有效的同步工具,尤其适用于那些需要等待一组线程全部完成后再执行后续操作的场景。在实际开发中,结合 `ExecutorService` 和 `Future`,我们可以构建更复杂的并发控制策略,以...
CountDownLatch通常用于多线程协作场景,例如启动一批任务,然后等待所有任务执行完毕后再进行下一步操作。 在CountDownLatch中,我们首先需要指定一个计数器,这个计数器表示需要完成的任务数量。每当一个线程完成...
本文将详细介绍CountDownLatch和CyclicBarrier的工作原理、使用场景以及如何在实际项目中应用它们。 CountDownLatch和CyclicBarrier是Java并发编程中两个非常有用的同步工具,它们在不同的场景下有着各自的优势。...
2. **如何避免死锁与饥饿**:在使用CountDownLatch时,要确保所有线程都能正确调用`countDown()`,避免因某个线程异常而无法达到零导致死锁。 3. **如何选择合适的同步工具**:根据场景选择,如果需要所有线程执行完...
在批量插入数据的场景下,可以创建一个CountDownLatch对象,初始化为线程的数量,每个线程处理完自己的数据后调用`countDown()`方法,主线程通过调用`await()`方法等待所有线程完成后再执行后续操作,如提交事务。...
CountDownLatch 的主要使用场景包括:实现最大程度的并行性、开始执行前等待 n 个线程完成各自任务、死锁检测等。在实际开发中,CountDownLatch 可以用来实现主线程和子线程之间的通信,例如主线程发送命令启动子...
在游戏场景中,如果四个玩家需要同时开始游戏,就可以使用CyclicBarrier。`CyclicBarrier(int parties, Runnable barrierAction)`构造函数接受两个参数,`parties`表示需要等待的线程数,`barrierAction`是所有线程...
CountDownLatch的使用场景: 1. 并发编程:CountDownLatch可以用于并发编程中,控制多个线程的执行顺序。 2. 数据处理:CountDownLatch可以用于数据处理中,等待多个线程处理数据的完成。 CountDownLatch的await()...
本文将深入探讨CountDownLatch的工作原理、使用方法以及它在并发编程中的应用场景。 1. **CountDownLatch简介** CountDownLatch是一个计数器,初始化时设置一个初始值,然后多个线程可以等待这个计数器归零。一旦...
如何使用CountDownLatch同步java多线程 CountDownLatch 是 Java 并发编程中的一种常用工具,用于控制多个线程的并发执行。它可以实现多个线程之间的同步操作,使得线程之间可以协调工作,避免了线程之间的互相干扰...
Java 使用 CountDownLatch 等待多线程全部执行完成 CountDownLatch 是 Java 中的一个同步工具类,允许一个或多个线程等待其他线程完成操作。它的应用场景非常广泛,例如在处理大量数据时,可以使用多线程的方式处理...
- **并发任务分阶段执行**:一个大型任务可以被分解成多个子任务,使用 CountDownLatch 让所有子任务完成后,再执行下一步操作。 5. **注意事项**: - `CountDownLatch` 不是线程安全的,因此计数器一旦递减到零...
3.2 使用CountDownLatch实现同步 主线程等待多个线程完成 4.1 场景介绍 4.2 使用CountDownLatch实现等待 CountDownLatch的其他应用场景 5.1 倒计时计时器 5.2 同时开始任务 5.3 等待多个资源就绪 CountDownLatch与...
本文将详细介绍 Java 并发工具类的四大类:CountDownLatch、Semaphore、CyclicBarrier 和 Phaser,及其应用场景和使用方法。 CountDownLatch CountDownLatch 是一个同步的辅助类,允许一个或多个线程,等待其他一...
- **场景二:** 使用 N (N ≥ 1) 初始化 CountDownLatch,可以让一个线程等待其他 N 个线程完成任务。 **实践案例:** 假设我们有 10 个线程需要对一组数据进行计算,这 10 个线程需要同时开始工作,并且只有当所有...