当前位置:网站首页>Asynchronous or thread pool
Asynchronous or thread pool
2022-06-23 18:13:00 【Catch wind and shadow】
public class ThreadTest {
public static ExecutorService executor = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws ExecutionException, InterruptedException {
// System.out.println("main......start.....");
// Thread thread = new Thread01();
// thread.start();
// System.out.println("main......end.....");
// Runable01 runable01 = new Runable01();
// new Thread(runable01).start();
// FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
// new Thread(futureTask).start();
// System.out.println(futureTask.get());
// service.execute(new Runable01());
// Future<Integer> submit = service.submit(new Callable01());
// submit.get();
System.out.println("main......start.....");
// CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
// System.out.println(" Current thread :" + Thread.currentThread().getId());
// int i = 10 / 2;
// System.out.println(" Running results :" + i);
// }, executor);
/** * Treatment after the completion of the method */
// CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
// System.out.println(" Current thread :" + Thread.currentThread().getId());
// int i = 10 / 0;
// System.out.println(" Running results :" + i);
// return i;
// }, executor).whenComplete((res,exception) -> {
// // Although you can get abnormal information , But you can't modify the returned data
// System.out.println(" The asynchronous task completed successfully ... The result is :" + res + " Exception is :" + exception);
// }).exceptionally(throwable -> {
// // Can sense exceptions , At the same time, return the default value
// return 10;
// });
/** * Method performs back-end processing */
// CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
// System.out.println(" Current thread :" + Thread.currentThread().getId());
// int i = 10 / 2;
// System.out.println(" Running results :" + i);
// return i;
// }, executor).handle((result,thr) -> {
// if (result != null) {
// return result * 2;
// }
// if (thr != null) {
// System.out.println(" The asynchronous task completed successfully ... The result is :" + result + " Exception is :" + thr);
// return 0;
// }
// return 0;
// });
/** * Thread serialization * 1、thenRunL: Unable to get the execution result of the previous step * 2、thenAcceptAsync: I can accept the result , But no return value * 3、thenApplyAsync: I can accept the result , There is a return value * */
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println(" Current thread :" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println(" Running results :" + i);
return i;
}, executor).thenApplyAsync(res -> {
System.out.println(" Mission 2 Launched the ..." + res);
return "Hello" + res;
}, executor);
System.out.println("main......end....." + future.get());
}
private static void threadPool() {
ExecutorService threadPool = new ThreadPoolExecutor(
200,
10,
10L,
TimeUnit.SECONDS,
new LinkedBlockingDeque<Runnable>(10000),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
// Thread pool for scheduled tasks
ExecutorService service = Executors.newScheduledThreadPool(2);
}
public static class Thread01 extends Thread {
@Override
public void run() {
System.out.println(" Current thread :" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println(" Running results :" + i);
}
}
public static class Runable01 implements Runnable {
@Override
public void run() {
System.out.println(" Current thread :" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println(" Running results :" + i);
}
}
public static class Callable01 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println(" Current thread :" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println(" Running results :" + i);
return i;
}
}
}
边栏推荐
- [WebSocket] 开发在线客服系统知识点-websocket返回状态码的含义
- How to make towel washing label
- Programmers are very useful ten tool websites, which are worth collecting
- README
- Latex compiled successfully but could not be output to PDF
- How to make a badge
- Latex使用\usepackage{hyperref}报错:paragraph ended before [email protected]@link was complete
- Implementing Domain Driven Design - using ABP framework - General guidelines
- New function! Qianfan magic pen apaas December capability monthly report
- VNC Viewer方式的远程连接树莓派
猜你喜欢

论文阅读 (58):Research and Implementation of Global Path Planning for Unmanned Surface Vehicle Based...

How to solve the problem that the esp8266-01s cannot connect to Huawei routers

org. apache. ibatis. binding. BindingException: Invalid bound statement (not found):...

Torch learning (I): environment configuration

【ESP8266-01s】获取天气,城市,北京时间

SimpleDateFormat在多线程环境下存在线程安全问题。

Paper reading (58):research and implementation of global path planning for unmanned surface vehicle based

csdn涨薪秘籍之Jenkins集成allure测试报告全套教程

Customer service system building tutorial_ Installation and use mode under the pagoda panel_ Docking with official account_ Support app/h5 multi tenant operation

iMeta | 南农沈其荣团队发布微生物网络分析和可视化R包ggClusterNet
随机推荐
Digital intelligent supply chain collaboration solution for new energy industry
论文阅读 (50):A Novel Matrix Game with Payoffs of Maxitive Belief Structure
科技互动沙盘是凭借什么收获人气的
POC about secureworks' recent azure Active Directory password brute force vulnerability
Which securities company is good for opening a mobile account? Is online account opening safe?
12. Manage network environment
[esp8266 - 01s] obtenir la météo, Ville, heure de Beijing
PostgreSQL series articles -- the world's most advanced open source relational database
TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
【ESP8266-01s】獲取天氣,城市,北京時間
How to use R language to draw scatter diagram
Dive Into Deep Learning——1、前言
论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
Latex compiled successfully but could not be output to PDF
January 5, 2022: there are four kinds of rhythms: AABB, ABAB and ABB
对抗攻击与防御 (2):对抗样本的反制策略
Kotlin practical skills you should know
[tool C] - lattice simulation test 2
6 steps to teach you financial data mining preprocessing
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...