线程池在项目中的使用

编程入门 行业动态 更新时间:2024-10-21 03:57:05

<a href=https://www.elefans.com/category/jswz/34/1771240.html style=线程池在项目中的使用"/>

线程池在项目中的使用

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());}
} 

更多推荐

线程池在项目中的使用

本文发布于:2023-12-07 01:19:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1669633.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!