线程池在项目中的使用

1.runAsync执行完后无返回值

package com.search.thread;
import java.util.concurrent.*;
public class ThreadTest {public static ExecutorService executor = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {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);}
}  

2.supplyAsync执行完后有返回值,后续能感知异常

package com.search.thread;
import java.util.concurrent.*;
public class ThreadTest {public static ExecutorService executor = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {/*** 方法完成后的处理*/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;});}
} 

3.supplyAsync执行完后有返回值,后续能感知异常

package com.search.thread;
import java.util.concurrent.*;
public class ThreadTest {public static ExecutorService executor = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {/*** 方法执行完后端处理*/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;});}
} 

4.线程串行化,如:A任务执行完再执行B任务

在这里插入图片描述

package com.search.thread;
import java.util.concurrent.*;
public class ThreadTest {public static ExecutorService executor = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {//1.thenRun开头的,不接受上一步(如:supplyAsync)的返回结果,thenRun执行完本身没有返回值CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {System.out.println("当前线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.println("运行结果:" + i);return i;}, executor).thenRunAsync(()-> {System.out.println("任务2启动了...");},executor);// 2.thenAccept开头的,需要接受上一步(如:supplyAsync)的返回结果,thenAccept执行完本身也没有返回值CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {System.out.println("当前线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.println("运行结果:" + i);return i;}, executor).thenAcceptAsync(res -> {System.out.println("任务2启动了..." + res);}, executor);// 3.thenApply开头的,需要上一步(如:supplyAsync)的返回结果,thenApply执行完本身最终会有返回值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);}
} 

5.两任务组合 - 如:A、B两任务都要完成再执行另一个任务C

在这里插入图片描述
在这里插入图片描述

package com.search.thread;
import java.util.concurrent.*;
public class ThreadTest {public static ExecutorService executor = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {// thenApply开头的,需要上一步(如:supplyAsync)的返回结果,thenApply执行完本身最终会有返回值CompletableFuture<String> future01 = 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);CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(() -> {System.out.println("当前线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.println("运行结果:" + i);return i;}, executor);//1.future01和future02执行完成后再执行自己的,不接受future01和future02的返回值,执行完自己的也没有返回值future01.runAfterBothAsync(future02,()->{System.out.println("任务3开始...");},executor);//2.future01和future02执行完成后再执行自己的,接受future01和future02的返回值,执行完自己的是没有返回值的future01.thenAcceptBothAsync(future02,(f1,f2)-{System.out.println("任务3开始...之前的返回结果:"+f1+"-->"+f2);},executor);//3.future01和future02执行完成后再执行自己的,接受future01和future02的返回值,执行完自己的是有返回值的CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) ->(return f1 +":" + f2 +" -> Haha";},executor);System.out.println("main....end...."+future.get());}
} 

6.两任务组合 - 如:A、B两任务只要有一个完成就执行另一个任务C

在这里插入图片描述
在这里插入图片描述

package com.search.thread;
import java.util.concurrent.*;
public class ThreadTest {public static ExecutorService executor = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {// thenApply开头的,需要上一步(如:supplyAsync)的返回结果,thenApply执行完本身最终会有返回值CompletableFuture<Object> future01 = 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);CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {System.out.println("当前线程:" + Thread.currentThread().getId());int i = 10 / 2;System.out.println("运行结果:" + i);return i;}, executor);//1.future01或future02有一个执行完成后再执行自己的任务,不接受future01和future02的返回值,执行完自己的也没有返回值future01.runAfterEitherAsync(future02,()->{System.out.println("任务3开始...");},executor);//2.future01或future02有一个执行完成后再执行自己的任务,接受future01和future02的返回值(返回类型要相同),执行完自己的是没有返回值的future01.acceptEitherAsync(future02,(res)->{System.out.printn("任务3开始...之前的结果:"+res);},executor);//3.future01或future02有一个执行完成后再执行自己的,接受future01和future02的返回值,执行完自己的是有返回值的CompletableFuture<String> future = future01.applyToEitherAsync(future02, res ->{System.out.println("任务3开始...之前的结果: + res");return res.toString() +"->哈哈"};executor)System.out.printIn("main....end..."+future .get());}
} 

7.多任务组合 - 等待全部完成和只要有一个任务完成

在这里插入图片描述
allOf:等待所有任务完成
anyOf:只要有一个任务完成

package com.search.thread;
import java.util.concurrent.*;
public class ThreadTest {public static ExecutorService executor = Executors.newFixedThreadPool(10);public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> futureImg= CompletableFuture.supplyAsync(() -> {System.out.println("查询商品的图片信息");return "hello.jpg";});CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {System.out.printIn("查询商品的属性");return "黑色+256G";});CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {try {Thread.sleep(3000);System.out.println("查询商品介绍"); }catch (InterruptedException e) {e.printStackTrace();}return"华为";});CompletableFuture<Void> all0f = CompletableFuture.allof(futureImg, futureAttr, futureDesc);allof.get();//等待所有结果完成System.out.printIn("main,.,end...,"+futureImg.get()+"=>"+futureAttr.get()+"=>"+futureDesc.get());CompletableFuture<0bject> anyof = CompletableFuture.any0f(futureImg, futureAttr, futureDescanyof.get();//等待所有结果完成System.out.printIn("main,.,end...,"+anyof.get());}
} 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/111757.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[架构之路-240]:目标系统 - 纵向分层 - 应用层 - 应用层协议与业务应用程序的多样化,与大自然生物的丰富多彩,异曲同工

目录 前言&#xff1a; - 倒金子塔结构 - 大自然的组成 一、应用层在计算机系统中的位置 1.1 计算机应用程序的位置 1.1.1 业务应用程序概述 1.1.2 应用程序的分类 - 按照计算机作用范围 1.1.3 业务应用程序分类 - 按照行业分类 1.2 网络应用协议的位置 1.2.1 网络协…

django建站过程(1)

django建站过程&#xff08;1&#xff09; 使用pycharm创建过程运行项目创建数据库创建超级用户登录生成的后台&#xff1a;界面本地化 准备以django,bootstrap来做一个过程记录&#xff0c;文章主要阐述过程的细节。 使用pycharm创建过程 创建项目“schoolapps”&#xff0c;…

【Java异常】什么是异常,Java中如何处理异常?

个人简介&#xff1a;Java领域新星创作者&#xff1b;阿里云技术博主、星级博主、专家博主&#xff1b;正在Java学习的路上摸爬滚打&#xff0c;记录学习的过程~ 个人主页&#xff1a;.29.的博客 学习社区&#xff1a;进去逛一逛~ Java异常处理 1. 了解异常&#xff1a;2. 异常…

探索未来的视觉革命:卷积神经网络的崭新时代(一)

&#x1f497;&#x1f497;&#x1f497;欢迎来到我的博客&#xff0c;你将找到有关如何使用技术解决问题的文章&#xff0c;也会找到某个技术的学习路线。无论你是何种职业&#xff0c;我都希望我的博客对你有所帮助。最后不要忘记订阅我的博客以获取最新文章&#xff0c;也欢…

unigui添加ssl(https)访问的方法

首先到腾讯云或者阿里云去申请免费的证书&#xff0c;前提是在该服务商那有申请过域名&#xff0c;怎么找出这个界面&#xff1f;网页顶部一般都有个搜索框&#xff0c;输入【证书】或者【SSL】就能看到了&#xff0c;然后点击申请免费证书&#xff0c;把解析信息填入自己的域名…

Allegro两种自动对齐方法

本法基于cadence的allegro可以在PCB设计运用&#xff0c;使用方法如下&#xff1a; 方式一&#xff1a;allegro软件的自动对齐——使用过程繁琐一点 1.在“setup”下拉选项中选择“application mode”&#xff0c;在副选项中选择“placement edit”&#xff1b; 2.在“Find”…

Mac电脑无法识别移动硬盘怎么办?

很多人都喜欢在Mac电脑上办公、学习&#xff0c;但有时我们将移动硬盘连接Mac电脑时&#xff0c;却会发现电脑无法识别移动硬盘。那么&#xff0c;Mac电脑无法识别移动硬盘怎么办呢&#xff1f; Mac无法识别移动硬盘的原因 导致Mac不识别移动硬盘的原因有很多&#xff0c;你可…

【Andriod】adb调试安卓手机时连接真机或模拟器的3种方法,你知道么?

一.adb是什么&#xff1f; adb 称之为&#xff1a;Android 调试桥 &#xff08;Android Debug Bridge &#xff09;是一种允许模拟器或已连接的 Android 设备进行通信的命令行工具&#xff0c;它可为各种设备操作提供便利&#xff0c;如安装和调试应用&#xff0c;并提供对 Un…

软考-网络安全体系与网络安全模型

本文为作者学习文章&#xff0c;按作者习惯写成&#xff0c;如有错误或需要追加内容请留言&#xff08;不喜勿喷&#xff09; 本文为追加文章&#xff0c;后期慢慢追加 by 2023年10月 网络安全体系相关安全模型 BLP机密性模型 BLP&#xff08;Biba-格雷泽-麦克拉伦&#x…

centos 7.9离线安装wget

1.下载安装包 登录到wget官网上下载最新的wget的rpm安装包到本地 http://mirrors.163.com/centos/7/os/x86_64/Packages/ 2.上传安装包到服务器 3.安装 rpm -ivh wget-1.14-18.el7_6.1.x86_64.rpm 4.查看版本 wget -V

Cesium Vue(七)— GEOJSON数据展示

1. GeoJSON GeoJSON 是一种用于对各种地理数据结构进行编码的格式。 简而言之&#xff0c;GeoJSON为你提供了一种简单的格式来表示简单的地理特征以及它们的非空间属性。 结构&#xff1a; {"type": "Feature","geometry": {"type"…

uniapp——自定义组件插槽及使用

案例样式 自定义组件pageBox.vue <template><view><view class"bgColor" :style"{ height: bgHeight rpx }"></view><view class"main"><!-- 主要内容放这里 --><slot></slot></view>&…

taro使用defineConstants定义全局变量eslint报错该变量不存在

问题描述 在taro项目中使用defineConstants定义一些全局变量供业务代码中进行使用&#xff0c;全局变量声明config/index.js代码如下&#xff1a; module.exports {defineConstants: {LOGIN_URL: JSON.stringify(/baidu/login), },全局变量使用代码如下&#xff1a; /*** 跳…

【开源分享】基于Html开发的房贷计算器,模仿新浪财经

房贷计算器是一种房贷计算的在线计算Web应用&#xff0c;按用户选择的贷款类型、贷款金额、期限、利率可计算得出每月月供参考、支付利息、还款总额这些信息。本文模仿新浪财经开发的房贷计算器。 作品预览 https://fangdai.gitapp.cn 源码地址 https://github.com/geeeeeee…

NFTScan 支持非 EVM 公链的 NFT Collection 的认证功能

截止到 2023 年 10 月份&#xff0c;NFTScan 已经支持了 18 条区块链网络&#xff0c;其中有 14 条 是 EVM 兼容的区块链网络&#xff1a; Ethereum、BNBChain、Polygon、zkSync、Base、Linea、Arbitrum、Optimism、Avalanche、Fantom、PlatON、Cronos、Gnosis、Moonbeam。 另…

Qt消息对话框的使用

本文介绍Qt消息对话框的使用。 QMessageBox类是Qt编程中常用到的一个类&#xff0c;主要用来进行一些简单的消息提示&#xff0c;比如&#xff1a;问题对话框&#xff0c;信息对话框等&#xff0c;这些对话框都属于QMessageBox类的静态方法&#xff0c;使用起来比较简单&#…

Linux性能优化--性能追踪3:系统级迟缓(prelink)

12.0 概述 本章包含的例子说明了如何用Linux性能工具寻找并修复影响整个系统而不是某个应用程序的性能问题。阅读本章后&#xff0c;你将能够&#xff1a; 追踪是哪一个进程导致了系统速度的降低。用strace调查一个不受CPU限制的进程的性能表现。用strace调查一个应用程序是如…

华为云Stack的学习(十)

十一、华为云Stack容器服务介绍 1.云容器引擎服务CCE 云容器引擎&#xff08;Cloud Container Engine&#xff0c;CCE&#xff09;提供高度可扩展的、高性能的企业级Kubernetes集群&#xff0c;支持运行Docker容器。借助云容器引擎&#xff0c;可以在云上轻松部署、管理和扩展…

element-plus 表格-自定义样式实现2

<template><h2>表格修改样式利用属性修改</h2><h3>row-style 行样式</h3><h3>row-style header-row-style 不能改背景色</h3><h3>cell-style header-cell-style能改背景色</h3><el-tableref"tableRef":dat…

低调而无为而治,藏在超级应用背后的道家哲学

众所周知&#xff0c;Elon Musk 想将 Twitter 重新设计定位成一款“超级应用 - X”的野心已经不再是秘密。伴随着应用商店中 Twitter 标志性的蓝鸟 Logo 被 X 取代后&#xff0c;赛博世界充满了对这件事情各种角度的探讨与分析。 Musk 曾经无数次通过微信这一样本来推广他的“超…