当前位置:网站首页>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]
边栏推荐
- 9、 Networking technology
- SQL developer graphical window to create database (tablespace and user)
- Understand the great changes of network security in five years
- Advanced socket programming (options and control information)
- IPv6表示方法与配置案例
- TCP socket communication experiment
- Complex floating point division of vivado IP core floating point
- Best example of amortized cost
- VMware虚拟机在物理机win10系统下如何连接外网
- 4、 LAN and man
猜你喜欢
随机推荐
网络安全学习(一)
Floating point square root of vivado IP core floating point
Handwritten digit recognition using neural network
案例补充、ATM
Those vulnerability attacks on app
What is DNS amplification attack
vmstat 内存消耗查询
day03_ 2_ task
5G控制面协议之N2接口
How to use SFTP command to access SFTP server on the development board
MQTT服务器搭建以及使用MQTT.fx测试
FPGA - odd even frequency division and decimal frequency division code routine
Condition 条件对象源码浅读
ss命令详解
day17_ Under collection
day15_ generic paradigm
Computer right mouse click always turn around what's going on
Embedding理解+代码
Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers
Complex floating point division of vivado IP core floating point








