线程线程池并行开发JAVA_使用ThreadPoolExecutor 创建线程池,完成并行操作

packagecom.hao.service;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.LinkedList;importjava.util.List;importjava.util.concurrent.Callable;importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Future;importjava.util.concurrent.TimeUnit;importjavax.annotation.PostConstruct;importorg.springframework.stereotype.Service;importcom.google.common.base.Preconditions;

@Servicepublic classExecutorContext {publicExecutorService executorService;private int DEFAULT_WAIT_SECONDS = 2;

@PostConstructpublic voidinit() {

executorService=ExecutorManager.threadPoolExecutor;

}public List waitAllFutures(List> calls, int milliseconds) throwsException {

Preconditions.checkArgument(null != calls && !calls.isEmpty(), "callable empty.");

LatchedCallables latchAndCallables =wrapCallables(calls);

List> futurres = new LinkedList<>();for (CountdownedCallablecallable : latchAndCallables.wrappedCallables) {if (null !=callable) {

futurres.add(executorService.submit(callable));

}

}

List rets = new ArrayList<>();if(latchAndCallables.latch.await(milliseconds, TimeUnit.MILLISECONDS)) {for (CountdownedCallablecall : latchAndCallables.wrappedCallables) {

rets.add(call.getResult());

}

}else{for (Futurefuture : futurres) {if (!future.isDone()) {

future.cancel(true);

}

}

}returnrets;

}public List waitAllCallables(List> calls, int seconds) throwsException {

Preconditions.checkArgument(null != calls && !calls.isEmpty(), "callable empty.");

LatchedCallables latchAndCallables =wrapCallables(calls);for (CountdownedCallablecallable : latchAndCallables.wrappedCallables) {

executorService.submit(callable);

}

List rets = new ArrayList<>();if(latchAndCallables.latch.await(seconds, TimeUnit.SECONDS)) {for (CountdownedCallablecall : latchAndCallables.wrappedCallables) {

rets.add(call.getResult());

}

}returnrets;

}public List waitAllCallables(@SuppressWarnings("unchecked") Callable... calls) throwsException {

Preconditions.checkNotNull(calls,"callable empty.");returnwaitAllCallables(Arrays.asList(calls), DEFAULT_WAIT_SECONDS);

}private static LatchedCallables wrapCallables(List>callables) {

CountDownLatch latch= newCountDownLatch(callables.size());

List> wrapped = new ArrayList<>(callables.size());for (Callablecallable : callables) {

wrapped.add(new CountdownedCallable<>(callable, latch));

}

LatchedCallables returnVal = new LatchedCallables<>();

returnVal.latch=latch;

returnVal.wrappedCallables=wrapped;returnreturnVal;

}public static class LatchedCallables{publicCountDownLatch latch;public List>wrappedCallables;

}public static class CountdownedCallable implements Callable{private final Callablewrapped;private finalCountDownLatch latch;privateT result;public CountdownedCallable(Callablewrapped, CountDownLatch latch) {this.wrapped =wrapped;this.latch =latch;

}

@Overridepublic T call() throwsException {try{

result=wrapped.call();returnresult;

}finally{

latch.countDown();

}

}publicT getResult() {returnresult;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值