java多线程编程之CountDownLatch

java.util.concurrent.CountDownLatch这个类里面的主要方法为:

1.countDown(),Decrements the count of the latch, releasing all waiting threads if the count reaches zero.

2.await(),Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.

//Runner.java
package com.cc;
import java.util.concurrent.CountDownLatch;

public class Runner implements Runnable{
	private CountDownLatch latch;
	private int index;

	public Runner(int index){
		this.index = index;
	}

	public Runner(CountDownLatch latch, int index){
		this.latch = latch;
		this.index = index;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			Thread.sleep(10);
			latch.countDown();
			System.out.println("latch countDown :"+this.index);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("latch countDown:"+this.index+"end");
	}
	public CountDownLatch getLatch() {
		return latch;
	}
	public void setLatch(CountDownLatch latch) {
		this.latch = latch;
	}

}

//test.java
package com.cc;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CountDownLatch latch = new CountDownLatch(10);
		ExecutorService execServ = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 10; i++) {
			execServ.submit(new Runner(latch, i));
		}
		try {
			Thread.sleep(1);
			latch.await();
			System.out.println("latch await through pass");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("--end--");
	}

}

  

//Runner.java
package com.cc;
import java.util.concurrent.CountDownLatch;

public class Runner implements Runnable{
	private CountDownLatch latch;
	private CountDownLatch backLatch;
	private int index;

	public Runner(int index){
		this.index = index;
	}

	public Runner(CountDownLatch latch, CountDownLatch backLatch, int index){
		this.latch = latch;
		this.backLatch = backLatch;
		this.index = index;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			System.out.println("latch countDown :"+this.index);
			latch.countDown();
			System.out.println("latch:"+this.index);
			backLatch.await();
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		System.out.println("latch countDown:"+this.index+"end");
	}
	public CountDownLatch getLatch() {
		return latch;
	}
	public void setLatch(CountDownLatch latch) {
		this.latch = latch;
	}

	public CountDownLatch getBackLatch() {
		return backLatch;
	}

	public void setBackLatch(CountDownLatch backLatch) {
		this.backLatch = backLatch;
	}

}

//Test.java
package com.cc;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CountDownLatch latch = new CountDownLatch(10);
		CountDownLatch backLatch = new CountDownLatch(1);
		ExecutorService execServ = Executors.newFixedThreadPool(10);
		for (int i = 0; i < 10; i++) {
			execServ.submit(new Runner(latch, backLatch,i));
		}
		try {
			latch.await();
			System.out.println("latch await through pass");
			backLatch.countDown();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("--end--");
	}

}

  

时间: 2024-10-13 22:24:12

java多线程编程之CountDownLatch的相关文章

java并发编程之CountDownLatch

一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化CountDownLatch.由于调用了 countDown() 方法,所以在当前计数到达零之前,await 方法会一直受阻塞.之后,会释放所有等待的线程,await 的所有后续调用都将立即返回.这种现象只出现一次--计数无法被重置.如果需要重置计数,请考虑使用 CyclicBarrier. CountDownLatch是一个通用同步工具,它有很多用途.将计数 1 初始化的CountDow

java并发编程之CountDownLatch与CyclicBarrier

CountDownLatch和CyclicBarrier是jdk concurrent包下非常有用的两个并发工具类,它们提供了一种控制并发流程的手段.本文将会提供一些应用场景,结合源码,对它们的具体实现以及如何使用做一个具体分析. CountDownLatch CountDownLatch允许一个或多个线程等待其他线程完成操作. CountDownLatch使用案例 需求:解析一个文件下多个txt文件数据,可以考虑使用多线程并行解析以提高解析效率.每一个线程解析一个文件里的数据,等到所有数据解析

java多线程编程之Future/FutureTask和Callable

有这样一种场景,用多线程发送数据到某个服务器,需要知道各个线程是否都发送成功,等所有线程都发送完成才能继续下一轮计算和发送.如果用传统的多线程方式,就需要启动多个线程,然后在每个线程中分别发送数据,外部通过某种方式等待各个线程全部都发送完成,再进行后面的计算等流程.这种实现方式的代码会比较臃肿,在java中提供了一种Callable+Future的方法,可以将异步的多线程调用变为同步方式. Callable 在java的多线程编程中,有Thread和Runnable两种方式来新建线程,其中Run

Java并发编程之CountDownLatch源码解析

一.导语 最近在学习并发编程原理,所以准备整理一下自己学到的知识,先写一篇CountDownLatch的源码分析,之后希望可以慢慢写完整个并发编程. 二.什么是CountDownLatch CountDownLatch是java的JUC并发包里的一个工具类,可以理解为一个倒计时器,主要是用来控制多个线程之间的通信.比如有一个主线程A,它要等待其他4个子线程执行完毕之后才能执行,此时就可以利用CountDownLatch来实现这种功能了. 三.简单使用 public static void mai

Java 多线程编程之:notify 和 wait 用法

wait 和 notify 简介 wait 和 notify 均为 Object 的方法: Object.wait() —— 暂停一个线程 Object.notify() —— 唤醒一个线程 从以上的定义中,我们可以了解到以下事实: 想要使用这两个方法,我们需要先有一个对象 Object. 在多个线程之间,我们可以通过调用同一个对象的wait()和notify()来实现不同的线程间的可见. 对象控制权(monitor) 在使用 wait 和 notify 之前,我们需要先了解对象的控制权(mon

java多线程编程之Semaphore

java.util.concurrent.Semaphore这个类里面的主要方法为: void acquire():Acquires a permit from this semaphore, blocking until one is available, or the thread isinterrupted. boolean tryAcquire():Acquires a permit from this semaphore, only if one is available at the

iOS多线程编程之NSOperation和NSOperationQueue的使用(转自容芳志专栏)

转自由http://blog.csdn.net/totogo2010/ 使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也熟悉Java,NSOperation就和java.lang.Runnable接口很相似.和Java的Runnable一样,NSOperation也是设计用来扩展的,只需继承重写NSOperation的一个方法main.相当与ja

Java并发编程之volatile的理解

Java并发编程之volatile关键字的理解 Java中每个线程都有自己的工作内存,类比于处理器的缓存,线程的工作内存中保存了被该线程使用到的变量的主内存的拷贝.线程读写变量都是直接在自己的工作内存中进行的,而何时刷新数据(指将修改的结果更新到主存或者把主存的变量读取覆盖掉工作内存中的值)是不确定的. volatile关键字是修饰字段的关键字,貌似是JDK1.5之后才有的,在多线程编程中,很大的几率会用到这个关键字,volatile修饰变量后该变量有这么一种效果:线程每一次读该变量都是直接从主

java并发编程之future模式

1.当你想并发去执行一段代码,但是还想获取这段代码的返回结果,那么future多线程模式就可以派上用场了,代码实现如下. public class Client { public Data request() { final FutureData futureData = new FutureData(); new Thread(new Runnable() { @Override public void run() { futureData.setRealData(new RealData()