当前位置:网站首页>异步or线程池
异步or线程池
2022-06-23 17:01:00 【捕風捉影】
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("当前线程:" + Thread.currentThread().getId());
// int i = 10 / 2;
// System.out.println("运行结果:" + i);
// }, executor);
/** * 方法完成后的处理 */
// CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
// System.out.println("当前线程:" + Thread.currentThread().getId());
// int i = 10 / 0;
// System.out.println("运行结果:" + i);
// return i;
// }, executor).whenComplete((res,exception) -> {
// //虽然能得到异常信息,但是没法修改返回数据
// System.out.println("异步任务成功完成了...结果是:" + res + "异常是:" + exception);
// }).exceptionally(throwable -> {
// //可以感知异常,同时返回默认值
// return 10;
// });
/** * 方法执行完后端处理 */
// CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
// System.out.println("当前线程:" + Thread.currentThread().getId());
// int i = 10 / 2;
// System.out.println("运行结果:" + i);
// return i;
// }, executor).handle((result,thr) -> {
// if (result != null) {
// return result * 2;
// }
// if (thr != null) {
// System.out.println("异步任务成功完成了...结果是:" + result + "异常是:" + thr);
// return 0;
// }
// return 0;
// });
/** * 线程串行化 * 1、thenRunL:不能获取上一步的执行结果 * 2、thenAcceptAsync:能接受上一步结果,但是无返回值 * 3、thenApplyAsync:能接受上一步结果,有返回值 * */
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}, executor).thenApplyAsync(res -> {
System.out.println("任务2启动了..." + 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()
);
//定时任务的线程池
ExecutorService service = Executors.newScheduledThreadPool(2);
}
public static class Thread01 extends Thread {
@Override
public void run() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
}
}
public static class Runable01 implements Runnable {
@Override
public void run() {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
}
}
public static class Callable01 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("当前线程:" + Thread.currentThread().getId());
int i = 10 / 2;
System.out.println("运行结果:" + i);
return i;
}
}
}
边栏推荐
- uniapp项目中防止用户重复提交
- How to make a shirt certificate
- Mobile SSH connection tool
- Troubleshooting and modification process of easycvr interface dislocation in small screen
- [learning notes] tidb learning notes (III)
- 论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
- What is the problem with TS File Error 404 when easynvr plays HLS protocol?
- [Hyperf]Entry “xxxInterface“ cannot be resolved: the class is not instantiable
- How to make validity table
- Baidu AI Cloud product upgrade Observatory in May
猜你喜欢

【Wwise】Wwise嵌入Unity后打包出现没有声音问题

《致敬百年巨匠 , 数藏袖珍书票》

Pause update Bulletin - walking Pikachu

Landing of global organizational structure control

论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...

微信小程序startLocationUpdateBackground()简单实现骑手配送位置

Video anomaly detection data set (shanghaitech)

esp8266-01s 不能连接华为路由器解决方法

JSON - learning notes (message converter, etc.)

csdn涨薪秘籍之Jenkins集成allure测试报告全套教程
随机推荐
Detailed explanation of ssl/tls principle and packet capturing
Single fire wire design series article 10: expanding application - single fire switch realizes double control
[esp8266 - 01s] obtenir la météo, Ville, heure de Beijing
Self taught programming introduction, what language to learn first?
6、VLAN
Read the typical application circuit of microphone
全局组织结构控制之抢滩登陆
How to make validity table
[learning notes] tidb learning notes (III)
如何通过线上股票开户?在线开户安全么?
《致敬百年巨匠 , 数藏袖珍书票》
High availability solution practice of mongodb advanced applications (4)
Similarities and differences between Chinese and American electronic signature SaaS
客服系统搭建教程_宝塔面板下安装使用方式_可对接公众号_支持APP/h5多租户运营...
How to solve the problem that the esp8266-01s cannot connect to Huawei routers
Paper reading (48):a Library of optimization algorithms for organizational design
Goframe framework: fast implementation of service end flow limiting Middleware
Deploy LNMP environment and install Typecho blog
Kotlin practical skills you should know
Digital intelligent supply chain collaboration solution for new energy industry