当前位置:网站首页>Callable 的使用
Callable 的使用
2022-07-29 05:27:00 【魔道不误砍柴功】
package com.xxl.job.admin.mytest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;
import java.util.stream.Collectors;
class Task implements Callable<String> {
private String taskName;
private ThreadPoolExecutor executorPool;
public List<Task> depencTasks = new ArrayList<>();
private int execTime = 0;
public Task(String taskName, ThreadPoolExecutor executorPool, int execTime) {
this.taskName = taskName;
this.executorPool = executorPool;
this.execTime = execTime;
}
@Override
public String call() throws Exception {
List<Future<String>> futures = depencTasks.stream().map(r -> executorPool.submit(r)).collect(Collectors.toList());
futures.forEach(r-> {
try {
r.get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
});
TimeUnit.SECONDS.sleep(execTime );
System.out.println("time=+"+new Date()+",taskName="+taskName+",thread:"+Thread.currentThread());
return "time"+new Date()+"+,taskName="+taskName;
}
}
public class ThreadPoll {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
4,
4,
0L,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(100),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
Task task1 = new Task("task1",threadPoolExecutor,0);
Task task2 = new Task("task2",threadPoolExecutor,1);
Task task3 = new Task("task3",threadPoolExecutor,1);
Task task4 = new Task("task4",threadPoolExecutor,1);
Task task5 = new Task("task5",threadPoolExecutor,10);
Task task6 = new Task("task6",threadPoolExecutor,10);
Task task7 = new Task("task7",threadPoolExecutor,1);
Task task8 = new Task("task8",threadPoolExecutor,1);
Task task9 = new Task("task9",threadPoolExecutor,1);
task1.depencTasks.add(task2);
task1.depencTasks.add(task3);
task1.depencTasks.add(task4);
task1.depencTasks.add(task5);
task1.depencTasks.add(task6);
task2.depencTasks.add(task7);
task3.depencTasks.add(task8);
task4.depencTasks.add(task9);
System.out.println("start time:"+new Date());
threadPoolExecutor.submit(task1);
}
}
运行结果:
start time:Tue Jul 19 16:23:57 CST 2022
time=+Tue Jul 19 16:23:58 CST 2022,taskName=task7,thread:Thread[pool-1-thread-4,5,main]
time=+Tue Jul 19 16:23:59 CST 2022,taskName=task2,thread:Thread[pool-1-thread-2,5,main]
time=+Tue Jul 19 16:24:09 CST 2022,taskName=task5,thread:Thread[pool-1-thread-2,5,main]
time=+Tue Jul 19 16:24:19 CST 2022,taskName=task6,thread:Thread[pool-1-thread-2,5,main]
time=+Tue Jul 19 16:24:20 CST 2022,taskName=task8,thread:Thread[pool-1-thread-2,5,main]
time=+Tue Jul 19 16:24:21 CST 2022,taskName=task3,thread:Thread[pool-1-thread-3,5,main]
time=+Tue Jul 19 16:24:21 CST 2022,taskName=task9,thread:Thread[pool-1-thread-2,5,main]
time=+Tue Jul 19 16:24:22 CST 2022,taskName=task4,thread:Thread[pool-1-thread-4,5,main]
time=+Tue Jul 19 16:24:22 CST 2022,taskName=task1,thread:Thread[pool-1-thread-1,5,main]
边栏推荐
- Right value reference and mobile construction
- FIR滤波器设计(1)——利用matlab的fdatool工具箱设计FIR滤波器参数
- etcd原理
- day06_ Classes and objects
- Solve the error that the simulation output is STX under the frequency division module Modelsim
- 软件包设置成——>YUM源
- 2022年的软件测试的岗位要求为何越来越高?这其中有什么不可告人的秘密吗?
- Hog+svm for pedestrian detection
- day16-集合上
- LVM逻辑卷组的管理
猜你喜欢

软件包设置成——>YUM源

【面试题】2022年最新软件测试面试题(400道)【附带答案】持续更新...

注解(Annotation)

10种常见的软件架构模式

Merkletree builds QT implementation UI

day13_ Under multithreading

How to use SFTP command to access SFTP server on the development board

Ram block memory generator of vivado IP core

2022年的软件测试的岗位要求为何越来越高?这其中有什么不可告人的秘密吗?

Hongke solution | a unique solution to realize seamless integration at low cost in Digital Substations
随机推荐
软件测试的优势有哪些?看看你了解多少.....
Leetcode question brushing record
Six common ways for hackers to attack servers
通过os-shell引发的mysql拿shell的思考
Multithreaded server programming
基于FPGA的IIR型滤波器设计
软件包设置成——>YUM源
多路IO用法
Joint use skills of joiner.on and stream().Map
day03_ 2_ task
How to judge whether a business is attacked by DDoS? What harm will it cause?
FPGA—奇偶数分频和小数分频代码例程
如何画出优秀的架构图
Sequence list and linked list
Solution for website being suspended
Ultra low cost DDoS attacks are coming. See how WAF protects Jedi
Day16 set
如何判断业务被DDoS攻击?又会造成哪些危害?
TCP based online dictionary
Solve the error that the simulation output is STX under the frequency division module Modelsim