实际的开发应该开发过程中,经常需要使用到多线程,而且大多时候需要获取到每个线程执行的结果,然后再执行剩下的业务逻辑。具体实现如下;
pom文件引用:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
线程任务处理类;
package com.ganinfo.test;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.Callable;
/**
* @author Shuyu.Wang
* @package:com.ganinfo.test
* @className:
* @description:
* @date 2018-10-28 19:35
**/
@Slf4j
public class AuthCallable implements Callable {
private AuthType authType;
/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
* @throws Exception if unable to compute a result
*/
@Override
public Object call() throws Exception {
if ("1".equals(authType.getType())){
log.info("方式一"+authType.getName()+"授权start");
Thread.sleep(8000);
log.info("方式一授权end");
return authType.getName();
}
if ("2".equals(authType.getType())){
log.info("方式二"+authType.getName()+"授权start");
Thread.sleep(7000);
log.info("方式二授权end");
return authType.getName();
}
if ("3".equals(authType.getType())){
log.info("方式三"+authType.getName()+"授权start");
Thread.sleep(5000);
log.info("方式三授权end");
return authType.getName();
}
if ("4".equals(authType.getType())){
log.info("方式四"+authType.getName()+"授权start");
Thread.sleep(3000);
log.info("方式四授权end");
return authType.getName();
}
if ("5".equals(authType.getType())){
log.info("方式五"+authType.getName()+"授权start");
Thread.sleep(1000);
log.info("方式五授权end");
return authType.getName();
}
return null;
}
public void setAuthType(AuthType authType) {
this.authType = authType;
}
}
引用的POJO类型:
package com.ganinfo.test;
import lombok.Data;
/**
* @author Shuyu.Wang
* @package:com.ganinfo.test
* @className:
* @description:
* @date 2018-10-28 19:34
**/
@Data
public class AuthType {
private String type;
private String name;
}
业务实现类:
package com.ganinfo.test;
import com.ganinfo.utils.GsonUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author Shuyu.Wang
* @package:com.ganinfo.test
* @className:
* @description:
* @date 2018-10-28 19:41
**/
@Service
@Slf4j
public class AuthService {
public void auth(String a) {
long start=System.currentTimeMillis();
List<String> list = new ArrayList<>();
int count=5;
try {
final CountDownLatch countDownLatch = new CountDownLatch(count);
ExecutorService executorService = Executors.newFixedThreadPool(8);
ListeningExecutorService listeningExecutorService = MoreExecutors.listeningDecorator(executorService);
for (int i = 1; i <= count; i++) {
AuthCallable authCallable = new AuthCallable();
AuthType authType = new AuthType();
authType.setType(String.valueOf(i));
authType.setName(String.valueOf(i) + "名称");
authCallable.setAuthType(authType);
ListenableFuture listenableFuture = listeningExecutorService.submit(authCallable);
Futures.addCallback(listenableFuture, new FutureCallback<String>() {
@Override
public void onSuccess(String name) {
log.info("授权结果" + name);
list.add(name);
countDownLatch.countDown();
}
@Override
public void onFailure(Throwable throwable) {
countDownLatch.countDown();
log.info("处理出错:", throwable);
}
});
}
try {
executorService.shutdown();
//shutdown调用后,不可以再submit新的task,已经submit的将继续执行。
if (!countDownLatch.await(15, TimeUnit.MINUTES)) {
log.info("超时的时候向线程池中所有的线程发出中断");
// 超时的时候向线程池中所有的线程发出中断(interrupted)。
executorService.shutdownNow();
}
} catch (InterruptedException e) {
e.printStackTrace();
executorService.shutdownNow();
//shutdownNow试图停止当前正执行的task,并返回尚未执行的task的list
}
log.info("执行结果" + GsonUtil.GsonString(list));
long end=System.currentTimeMillis();
log.info("用时" + (end-start));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
执行结果如下:
2018-10-28 20:16:49.510 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-1] com.ganinfo.test.AuthCallable : 方式一1名称授权start
2018-10-28 20:16:49.522 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-2] com.ganinfo.test.AuthCallable : 方式二2名称授权start
2018-10-28 20:16:49.522 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-3] com.ganinfo.test.AuthCallable : 方式三3名称授权start
2018-10-28 20:16:49.522 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-4] com.ganinfo.test.AuthCallable : 方式四4名称授权start
2018-10-28 20:16:49.522 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-5] com.ganinfo.test.AuthCallable : 方式五5名称授权start
2018-10-28 20:16:50.523 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-5] com.ganinfo.test.AuthCallable : 方式五授权end
2018-10-28 20:16:50.523 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-5] com.ganinfo.test.AuthService : 授权结果5名称
2018-10-28 20:16:52.523 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-4] com.ganinfo.test.AuthCallable : 方式四授权end
2018-10-28 20:16:52.523 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-4] com.ganinfo.test.AuthService : 授权结果4名称
2018-10-28 20:16:54.523 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-3] com.ganinfo.test.AuthCallable : 方式三授权end
2018-10-28 20:16:54.523 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-3] com.ganinfo.test.AuthService : 授权结果3名称
2018-10-28 20:16:56.523 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-2] com.ganinfo.test.AuthCallable : 方式二授权end
2018-10-28 20:16:56.523 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-2] com.ganinfo.test.AuthService : 授权结果2名称
2018-10-28 20:16:57.512 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-1] com.ganinfo.test.AuthCallable : 方式一授权end
2018-10-28 20:16:57.512 INFO [alarmservice1,,,] 34204 --- [ool-13-thread-1] com.ganinfo.test.AuthService : 授权结果1名称
2018-10-28 20:16:57.515 INFO [alarmservice1,,,] 34204 --- [ main] com.ganinfo.test.AuthService : 执行结果["5名称","4名称","3名称","2名称","1名称"]
2018-10-28 20:16:57.515 INFO [alarmservice1,,,] 34204 --- [ main] com.ganinfo.test.AuthService : 用时8013
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/15840.html