当前位置:网站首页>Use of callable
Use of callable
2022-07-29 06:47:00 【The evil way does not miss the firewood cutting skill】
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);
}
}
Running results :
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]
边栏推荐
- 三、广域通信网
- Floating point addition and subtraction method of vivado IP core floating point
- day14_ Unit test & Date common class & String common class
- Network Security Learning (I)
- 华为交换机CE12808导入导出配置文件
- 软件包设置成——>YUM源
- day10_ Exception handling & enumeration
- Instant 新日期类的使用 API
- finally 和 return 的执行顺序
- day12_ Multithreading
猜你喜欢

MySQL 事物四种隔离级别分析

Network Security Learning (II)

Complex floating point division of vivado IP core floating point

day06_ Classes and objects

循环神经网络RNN

STP spanning tree principle and example of election rules

centos 部署postgresql 13

Hongke shares | why EtherCAT is the best solution to improve the performance of the control system?

软件定义边界SDP

day02_ Basic grammar
随机推荐
Complex floating point multiplication of vivado IP core floating point
Computer right mouse click always turn around what's going on
SDN拓扑发现原理
为什么5G N2接口控制面使用SCTP协议?
day03_2_作业
Embedding理解+代码
ping 原理
Is it OK to directly compare the size of two numbers in FPGA?
3、 Wide area communication network
网站被挂马的解决方案
Floating point square root of vivado IP core floating point
day03_1_流程控制
CNAME记录和A记录的区别
day03_ 2_ task
How to use SFTP command to access SFTP server on the development board
How to judge whether a business is attacked by DDoS? What harm will it cause?
On defect description style
会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读
8、 Network security
Condition 条件对象源码浅读