AtomicBoolean它允许一个线程等待一个线程完成任务,然后运行:

A boolean value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic variables. An AtomicBoolean is used in applications such as atomically updated flags, and cannot be used as a replacement for a Boolean.
public static void main(String[] args) {
Thread t2 = new Thread(new BarWorker("bb"));
Thread t1 = new Thread(new BarWorker("aa"));
t2.run();
t1.run();
} private static class BarWorker implements Runnable { private static AtomicBoolean exists = new AtomicBoolean(false); private String name; public BarWorker(String name) {
this.name = name;
} public void run() {
if (exists.compareAndSet(false, true)) { //当第一个线程设置为true后,另外的线程是进不来的 System.out.println(name + " enter"+"currentvalue="+exists.get());
try {
System.out.println(name + " working");
Thread.sleep(2000);
} catch (InterruptedException e) {
// do nothing
}
System.out.println(name + " leave");
exists.set(false);
} else {
System.out.println(name + " give up");
}
} }

打印的结果:

bb entercurrentvalue=true
bb working
bb leave
aa entercurrentvalue=true
aa working
aa leave

CountDownLatch

一个同步辅助类。在完毕一组正在其它线程中运行的操作之前,它同意一个或多个线程一直等待。

假设设置  final CountDownLatch end = new CountDownLatch(10);  end.countDown();能够降低计数

假设在某个地方写  end.await();  假设计数不为0,全部线程会一直等待,计数不会被重置

private static  CountDownLatch mLatch = new CountDownLatch(5);

	public static void main(String[] args) throws InterruptedException {

        final ExecutorService exec = Executors.newFixedThreadPool(10);  

        for (int index = 0; index < 5; index++) {
final int NO = index + 1;
Runnable run = new Runnable() {
public void run() {
try {
System.out.println(NO + " working");
Thread.sleep(2000);
} catch (InterruptedException e) {
} finally {
mLatch.countDown();
}
}
};
exec.submit(run); }
mLatch.await();
System.out.println("finish");
exec.shutdown();
}

结果:

1 working
3 working
2 working
4 working
5 working
finish

版权声明:本文博客原创文章,博客,未经同意,不得转载。

最新文章

  1. Linux TOP 交互命令
  2. 项目分布式部署那些事(1):ONS消息队列、基于Redis的Session共享,开源共享
  3. UICollectionView瀑布流的实现原理(转)
  4. 基于Microsoft Azure、ASP.NET Core和Docker的博客系统
  5. 2016年12月5日 星期一 --出埃及记 Exodus 20:26
  6. Hbase0.98的环境搭建
  7. Problem B 队列
  8. Windows 7 32位上硬盘安装linux[ubuntu13.04] 双系统
  9. WebActivatorEx
  10. 在ASP.Net MVC 中如何实现跨越Session的分布式TempData
  11. Golang使用pprof和qcachegrind进行性能监控
  12. _1Python简介 安装及版本检测
  13. salesforce lightning零基础学习(一) lightning简单介绍以及org开启lightning
  14. CAP原则
  15. BZOJ1855 股票交易 单调队列优化 DP
  16. Android图片处理(Matrix,ColorMatrix) - 转载
  17. python中hasattr, getattr,setattr及delattr四个方法
  18. TCC(Tiny C Compiler)介绍
  19. 【洛谷】P2983 [USACO10FEB]购买巧克力Chocolate Buying(贪心)
  20. Angular2快速入门-4.创建一个服务(创建NewsService提供数据)

热门文章

  1. Windows Phone开发(47):轻松调用Web Service
  2. Windows Phone开发(35):使用Express Blend绘图
  3. 【翻译】Why JavaScript Is and Will Continue to Be the First Choice of Programmers
  4. Mac maven环境变量配置
  5. oracle迁移mysql数据库注意(转)
  6. MySQL在大数据Limit使用
  7. 手把手教popupWindow从下往上,以达到流行效果
  8. 输入框 js正则推断输入
  9. Android 动画具体解释View动画
  10. Mahout推荐算法ItemBased