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

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

有几种方式:
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();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值