当前位置:首页 » 其他

多个线程到达后才能执行某个任务,并且只能执行一次

2015-04-02 23:51 本站整理 浏览(8)

有一种场景:多个线程到达(比如合并多个线程返回的结果)后才能执行某个任务,并且只能执行一次。有几种方式:

1、Thread的join,不再讲解,因为使用不方便,也是不建议使用的方式。

2、AtomicInteger ,其increaseAndGet 是非常方便实现这个需求的。

3、CountDownLatch ,这个组件也可以,并且在特定场景下,这个是最好的实现,比如有时间等待限制的。

下面看这个2 和 3的case。

import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 
 * @author xinchun.wang
 * @email: 532002108@qq.com
 * @createTime 2015-4-2 下午11:25:26
 */
public class AtomicExecute {
	private static final int all = 3;

	public static void main(String[] args) throws Exception {
		Vector<Integer> vector = new Vector<Integer>();
		AtomicInteger mask = new AtomicInteger(0);
		TestThread t1 = new TestThread(mask, vector, 1, 10);
		TestThread t2 = new TestThread(mask, vector, 11, 20);
		TestThread t3 = new TestThread(mask, vector, 21, 30);
		t1.start();
		t2.start();
		t3.start();

		Vector<Integer> vectorMain = new Vector<Integer>();
		for (int i = 1; i <= 30; i++) {
			vectorMain.add(i * i * i);
		}
		System.out.println("vectorMain: " + addVector(vectorMain));

	}

	private static long addVector(Vector<Integer> vector) {
		long result = 0;
		for (Integer item : vector) {
			result = result + item;
		}
		return result;
	}

	public static class TestThread extends Thread {
		private final AtomicInteger mask;
		private final Vector<Integer> vector;
		private int begin;
		private int end;

		public TestThread(AtomicInteger mask, Vector<Integer> vector,
				int begin, int end) {
			this.mask = mask;
			this.vector = vector;
			this.begin = begin;
			this.end = end;
		}

		@Override
		public void run() {
			for (int i = begin; i <= end; i++) {
				vector.add(i * i * i);
			}
			// do some things
			if (mask.incrementAndGet() == all) {
				System.out.println("vector: " + addVector(vector));
			}
		}
	}

}
/**
 * 
 * @author xinchun.wang
 * @email: 532002108@qq.com
 * @createTime 2015-4-2 下午11:25:26
 */
public class CountDown {
	private static final int all = 3;

	public static void main(String[] args) throws Exception {
		Vector<Integer> vector = new Vector<Integer>();
		CountDownLatch mask = new CountDownLatch(all);
		TestThread t1 = new TestThread(mask, 1, 10, vector);
		TestThread t2 = new TestThread(mask, 11, 20, vector);
		TestThread t3 = new TestThread(mask, 21, 30, vector);
		TestThread2 t4 = new TestThread2(mask, vector);
		t4.start();
		t1.start();
		t2.start();
		t3.start();

		Vector<Integer> vectorMain = new Vector<Integer>();
		for (int i = 1; i <= 30; i++) {
			vectorMain.add(i * i * i);
		}
		System.out.println("vectorMain: " + addVector(vectorMain));
	}

	private static long addVector(Vector<Integer> vector) {
		long result = 0;
		for (Integer item : vector) {
			result = result + item;
		}
		return result;
	}

	/**
	 * 输出结果的线程
	 */
	public static class TestThread2 extends Thread {
		private final CountDownLatch mask;
		private final Vector<Integer> vector;

		public TestThread2(CountDownLatch mask, Vector<Integer> vector) {
			this.mask = mask;
			this.vector = vector;
		}

		@Override
		public void run() {
			try {
				mask.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("vector: " + addVector(vector));
		}
	}

	/**
	 * 实际添加数据的线程
	 */
	public static class TestThread extends Thread {
		private final CountDownLatch mask;
		private int begin;
		private int end;
		private Vector<Integer> vector;

		public TestThread(CountDownLatch mask, int begin, int end,
				Vector<Integer> vector) {
			this.mask = mask;
			this.begin = begin;
			this.end = end;
			this.vector = vector;
		}

		@Override
		public void run() {
			for (int i = begin; i <= end; i++) {
				vector.add(i * i * i);
			}
			mask.countDown();
		}
	}
}