java-如何杀死CompletableFuture相关线程?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何杀死CompletableFuture相关线程?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3025字,纯文字阅读大概需要5分钟。
内容图文
我有检查CompletableFuture执行时间的方法.如果这样的CompletableFuture执行超过2秒钟,我想终止此任务.但是,如果我没有执行CompletableFuture方法的控制线程,该怎么办?
final CompletableFuture<List<List<Student>>> responseFuture = new CompletableFuture<>();
responseFuture.supplyAsync(this::createAllRandomGroups)
.thenAccept(this::printGroups)
.exceptionally(throwable -> {
throwable.printStackTrace();
return null;
});
createAllRandomGroups()
private List<List<Student>> createAllRandomGroups() {
System.out.println("XD");
List<Student> allStudents = ClassGroupUtils.getActiveUsers();
Controller controller = Controller.getInstance();
List<List<Student>> groups = new ArrayList<>();
int groupSize = Integer.valueOf(controller.getGroupSizeComboBox().getSelectionModel().getSelectedItem());
int numberOfGroupsToGenerate = allStudents.size() / groupSize;
int studentWithoutGroup = allStudents.size() % groupSize;
if (studentWithoutGroup != 0) groups.add(this.getListOfStudentsWithoutGroup(allStudents, groupSize));
for(int i = 0; i < numberOfGroupsToGenerate; i++) {
boolean isGroupCreated = false;
while (!isGroupCreated){
Collections.shuffle(allStudents);
List<Student> newGroup = this.createNewRandomGroupOfStudents(allStudents, groupSize);
groups.add(newGroup);
if (!DataManager.isNewGroupDuplicated(newGroup.toString())) {
isGroupCreated = true;
allStudents.removeAll(newGroup);
}
}
}
DataManager.saveGroupsToCache(groups);
return groups;
}
printGroups()
private void printGroups(List<List<Student>> lists) {
System.out.println(lists);
}
该语句responseFuture.cancel(true);不会杀死responseFuture正在执行方法的线程.那么终止CompletableFuture线程最优雅的方法是什么?
解决方法:
当您创建诸如b = a.thenApply(function)之类的CompletableFuture阶段链时,此便捷的方法将创建不同组件的设置.基本上,这些组件彼此称为→功能→b,因此a的完成将触发对功能的评估,该评估将首先预先检查b是否仍未完成,然后评估您的功能并尝试使用b来完成b结果.
但是b本身不了解函数或对其进行评估的线程.实际上,函数不是b所特有的,任何人都可以调用complete,completeExceptionally或从任何线程中取消它,第一个获胜.因此,在类名中是可完成的.
接触评估函数的线程的唯一方法是从一开始就控制它们.
ExecutorService myWorkers = Executors.newFixedThreadPool(2);
CompletableFuture<FinalResultType> future
= CompletableFuture.supplyAsync(() -> generateInitialValue(), myWorkers)
.thenApplyAsync(v -> nextCalculation(v), myWorkers)
.thenApplyAsync(v -> lastCalculation(v), myWorkers);
future.whenComplete((x,y) -> myWorkers.shutdownNow());
现在,未来的完成,例如通过取消,将确保该链不会触发新的评估,并进一步尝试中断正在进行的评估(如果有).
因此您可以实施超时,例如
try {
try {
FinalResultType result = future.get(2, TimeUnit.SECONDS);
System.out.println("got "+result);
}
catch(TimeoutException ex) {
if(future.cancel(true)) System.out.println("cancelled");
else System.out.println("got "+future.get());
}
}
catch(ExecutionException|InterruptedException ex) {
ex.printStackTrace();
}
并不是说由于线程池关闭而导致的任务拒绝可能导致某些中间的未来永远无法完成,但是对于这一系列阶段而言,这是无关紧要的.重要的是,最终阶段的未来是完整的,这是有保证的,因为它的完成会触发关闭.
内容总结
以上是互联网集市为您收集整理的java-如何杀死CompletableFuture相关线程?全部内容,希望文章能够帮你解决java-如何杀死CompletableFuture相关线程?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。