JAVA对多线程的两个有用的辅助类(CountDownLatch和AtomicBoolean)
2014-08-07 15:36
429 查看
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.
打印的结果:
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
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
相关文章推荐
- JAVA多线程两个实用的辅助类(CountDownLatch和AtomicBoolean)
- Java多线程——6 并发流程控制CountDownLatch、CycliBarrier
- Java:多线程等待所有线程结束(CountDownLatch/CyclicBarrier)
- Java:多线程等待所有线程结束(CountDownLatch/CyclicBarrier)
- java多线程组件一:CountDownLatch使用方法的总结
- Java多线程(八)之Semaphore、CountDownLatch、CyclicBarrier、Exchanger
- Java多线程之CountDownLatch
- Java5 多线程(六)--CountDownLatch 同步工具类
- java多线程编程——同步器CountDownLatch(三)
- Java多线程之CountDownLatch、CyclicBarrier和Semaphore
- Java多线程编程之CountDownLatch同步工具使用实例
- Java:多线程等待所有线程结束(CountDownLatch/CyclicBarrier) .
- Java多线程之CountDownLatch
- java多线程之CountDownLatch倒数闸门
- Java 多线程 CountDownLatch 试用
- Java5 多线程(六)--CountDownLatch 同步工具类
- Java:多线程等待所有线程结束(CountDownLatch/CyclicBarrier) .
- Java多线程之CountDownLatch
- java countDownLatch 线程辅助类
- Java多线程之CountDownLatch学习