前言
Future 的基本知识
try {
FutureTask task = new FutureTask(new myCall());
Thread t1 = new Thread(task,"t1");
t1.start();
//System.out.println(task.get());
task.get(1,TimeUnit.SECONDS);
}catch (Exception e){
e.printStackTrace();
}finally {
// executorService.shutdown();
}
System.out.println("main");
class myCall implements Callable<String>{
@Override
public String call() throws Exception {
System.out.println(Thread.currentThread().getName());
while (true){
System.out.println("----");
Thread.sleep(1000);
if(Thread.currentThread().isInterrupted()){
break;
}
}
return "aa";
}
}
CompletableFuture对Future的改进
案例精讲-从电商网站的比价需求谈起
比较简单不赘述了
CompletableFuture常用方法
package com.wfg.futrue;
import java.util.concurrent.*;
/**
* @author wufagang
* @description
* @date 2022年07月02日 19:28
*/
public class CompletableFutureDemo {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(3);
// CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
// System.out.println("33333");
// try {
// TimeUnit.SECONDS.sleep(2);
// System.out.println(Thread.currentThread().getName());
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// },executorService);
// System.out.println(voidCompletableFuture.get());
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
int i = 1/0;
return "aa";
},executorService).whenComplete((v, e) -> {
if (e == null) {
System.out.println("计算完成,返回结果:" + v);
}
}).exceptionally(e -> {
System.out.println("异常信息" + e.getMessage());
return null;
});
//TimeUnit.SECONDS.sleep(2);
System.out.println("main。。。。。。");
executorService.shutdown();
}
}
package com.wfg.futrue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
/**
* @author wufagang
* @description
* @date 2022年07月02日 19:28
*/
public class CompletableFutureDemo2 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
m7();
}
public static void m7() throws ExecutionException, InterruptedException {
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
try {
System.out.println("A come in");
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 10;
}).thenCombine(CompletableFuture.supplyAsync(() -> {
try {
System.out.println("b com in ");
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 30;
}), (x, y) -> {
return x + y;
});
System.out.println(integerCompletableFuture.get());
}
public static void m6() throws ExecutionException, InterruptedException {
CompletableFuture<String> c1 = CompletableFuture.supplyAsync(() -> {
System.out.println("A come in");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "A";
});
CompletableFuture<String> c2 = CompletableFuture.supplyAsync(() -> {
System.out.println("B come in");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "B";
});
CompletableFuture<String> c3 = c1.thenCombine(c2, (e, f) -> {
return e+f;
});
System.out.println(c3.get());
}
public static void m5() throws ExecutionException, InterruptedException {
CompletableFuture<String> c1 = CompletableFuture.supplyAsync(() -> {
System.out.println("A come in");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "A";
});
CompletableFuture<String> c2 = CompletableFuture.supplyAsync(() -> {
System.out.println("B come in");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "B";
});
CompletableFuture<String> c3 = c1.applyToEither(c2, f -> {
return f;
});
System.out.println(c3.get());
}
public static void m4(){
ExecutorService executorService = Executors.newFixedThreadPool(3);
try {
CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
},executorService).thenRun(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}).thenRunAsync(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}).thenRun(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
});
System.out.println(c1.get());
}catch (Exception e){
e.printStackTrace();
}finally {
executorService.shutdown();
}
}
public static void m3(){
ExecutorService executorService = Executors.newFixedThreadPool(3);
try {
CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
},executorService).thenRun(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}).thenRun(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}).thenRun(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
});
System.out.println(c1.get());
}catch (Exception e){
e.printStackTrace();
}finally {
executorService.shutdown();
}
}
public static void m2(){
ExecutorService executorService = Executors.newFixedThreadPool(3);
try {
CompletableFuture<Void> c1 = CompletableFuture.runAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}).thenRun(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}).thenRun(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
}).thenRun(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName());
});
System.out.println(c1.get());
}catch (Exception e){
e.printStackTrace();
}finally {
executorService.shutdown();
}
}
public static void m1() throws Exception{
ExecutorService executorService = Executors.newFixedThreadPool(3);
// CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
// System.out.println("33333");
// try {
// TimeUnit.SECONDS.sleep(2);
// System.out.println(Thread.currentThread().getName());
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// },executorService);
// System.out.println(voidCompletableFuture.get());
CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
int i = 1/0;
return "aa";
},executorService).whenComplete((v, e) -> {
if (e == null) {
System.out.println("计算完成,返回结果:" + v);
}
}).exceptionally(e -> {
System.out.println("异常信息" + e.getMessage());
return null;
});
System.out.println();
//TimeUnit.SECONDS.sleep(2);
System.out.println("main。。。。。。");
executorService.shutdown();
completableFuture.get();
completableFuture.join();
}
}