package com.buba.springbootdemo.thread;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
*/
public class Demo {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
/**
* 异步执行,无返回值
* public static CompletableFuture runAsync(Runnable runnable)
* public static CompletableFuture runAsync(Runnable runnable,Executor executor)
*/
CompletableFuture future1 = CompletableFuture.runAsync(() -> System.out.println("线程执行"), executor);
/**
* 有返回值
* public static CompletableFuture supplyAsync(Supplier supplier)
* public static CompletableFuture supplyAsync(Supplier supplier,Executor executor)
*/
CompletableFuture future2 = CompletableFuture.supplyAsync(() -> 1, executor);
//返回1
System.out.println(future2.get());
}
}
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
/**
* whenComplete当前面的线程执行完后执行的操作,没有async的方法表示跟前面的线程用同一个线程执行,加async的方法表示使用新的线程执行
* exceptionally,表示如果出现异常的话执行的方法,没有异常不会执行,并且它可以修改返回值,而whenComplete不可以修改返回值
*
* public CompletableFuture whenComplete(BiConsumer super T, ? super Throwable> action)
* public CompletableFuture whenCompleteAsync(BiConsumer super T, ? super Throwable> action, Executor executor)
* public CompletableFuture exceptionally(Function fn)
*
* 返回的结果:null,异常信息:java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
* java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
* 10
*/
CompletableFuture future1 = CompletableFuture
.supplyAsync(() -> 1/0, executor)
.whenCompleteAsync((result,exception)-> System.out.println("返回的结果:"+result+",异常信息:"+exception),executor)
.exceptionally((ex)->{
System.out.println(ex);
return 10;
});
System.out.println(future1.get());
}
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
/**
*
* handle方法跟whenComplete一样都是当之前的线程执行完再执行,但是它可以修改返回的结果
*
* public CompletableFuture handle(BiFunction super T, Throwable, ? extends U> fn)
* public CompletableFuture handleAsync(BiFunction super T, Throwable, ? extends U> fn)
* public CompletableFuture handleAsync(BiFunction super T, Throwable, ? extends U> fn, Executor executor)
*
* 返回结果:1,异常信息:null
* 2
*/
CompletableFuture future2 = CompletableFuture
.supplyAsync(() -> 1, executor)
.handleAsync((result,exception)->{
System.out.println("返回结果:"+result+",异常信息:"+exception);
return 2;
},executor);
System.out.println(future2.get());
}
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
/**
* thenRun等之前线程执行完再执行,获取不到之前线程返回结果,自己当前线程也没有返回结果
* public CompletableFuture thenRun(Runnable action)
* public CompletableFuture thenRunAsync(Runnable action)
* public CompletableFuture thenRunAsync(Runnable action,Executor executor)
*/
CompletableFuture future1 = CompletableFuture
.supplyAsync(() -> 1, executor)
.thenRunAsync(() -> System.out.println("后续线程执行"), executor);
/**
* thenAccept等之前线程执行完再执行,能获取到之前线程返回结果,自己当前线程没有返回结果
* public CompletableFuture thenAccept(Consumer super T> action)
* public CompletableFuture thenAcceptAsync(Consumer super T> action)
* public CompletableFuture thenAcceptAsync(Consumer super T> action,Executor executor)
*/
CompletableFuture future2 = CompletableFuture
.supplyAsync(() -> 1, executor)
.thenAcceptAsync((result) -> System.out.println("后续线程执行,前面线程执行结果:" + result), executor);
/**
* thenApply等之前线程执行完再执行,能获取到之前线程返回结果,自己当前线程有返回结果
* public CompletableFuture thenApply(Function super T,? extends U> fn)
* public CompletableFuture thenApplyAsync(Function super T,? extends U> fn)
* public CompletableFuture thenApplyAsync(Function super T,? extends U> fn, Executor executor)
*/
CompletableFuture future3 = CompletableFuture
.supplyAsync(() -> 1, executor)
.thenApplyAsync((result) -> {
System.out.println("后续线程执行,前面线程执行结果:" + result);
return 2;
}, executor);
System.out.println(future3.get());
/**
* 后续线程执行
* 后续线程执行,前面线程执行结果:1
* 后续线程执行,前面线程执行结果:1
* 2
*/
}
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
CompletableFuture future1 = CompletableFuture
.supplyAsync(() -> {
System.out.println("第一个线程执行完了");
return 1;
}, executor);
/**
*
* runAfterBoth等前面两个线程都执行完,再执行的操作,获取不到前面的返回结果,但自己无返回结果
* thenAcceptBoth等前面两个线程都执行完,再执行的操作,可以获取到前面的返回结果,但自己无返回结果
* thenCombine等前面两个线程都执行完,再执行的操作,可以获取到前面的返回结果,自己有返回结果
*/
CompletableFuture
.runAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("第二个线程执行完了");
} catch (InterruptedException e) {
e.printStackTrace();
}
}, executor)
.runAfterBothAsync(future1, () -> System.out.println("前面两个都执行完了执行的"), executor);
CompletableFuture
.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("第二个线程执行完了");
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}, executor)
.thenAcceptBothAsync(future1, (result1,result2) -> System.out.println("前面两个都执行完了执行的,返回结果1:"+result1+",返回结果2:"+result2), executor);
CompletableFuture future3 = CompletableFuture
.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("第二个线程执行完了");
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}, executor)
.thenCombineAsync(future1, (result1, result2) -> {
System.out.println("前面两个都执行完了执行的,返回结果1:" + result1 + ",返回结果2:" + result2);
return 3;
}, executor);
System.out.println(future3.get());
}
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
CompletableFuture future1 = CompletableFuture
.supplyAsync(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("第一个线程执行完了");
return 1;
}, executor);
/**
*
* runAfterEitherAsync等前面两个线程中其中一个执行完,再执行的操作,获取不到前面的返回结果,但自己无返回结果
* acceptEitherAsync等前面两个线程中其中一个执行完,再执行的操作,可以获取到前面的返回结果,但自己无返回结果
* applyToEitherAsync等前面两个线程中其中一个执行完,再执行的操作,可以获取到前面的返回结果,自己有返回结果
*/
CompletableFuture
.runAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("第二个线程执行完了");
} catch (InterruptedException e) {
e.printStackTrace();
}
}, executor)
.runAfterEitherAsync(future1, () -> System.out.println("前面两个都执行完了执行的"), executor);
CompletableFuture
.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("第二个线程执行完了");
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}, executor)
.acceptEitherAsync(future1, (result1) -> System.out.println("前面两个都执行完了执行的,返回结果1:"+result1), executor);
CompletableFuture future3 = CompletableFuture
.supplyAsync(() -> {
try {
Thread.sleep(3000);
System.out.println("第二个线程执行完了");
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}, executor)
.applyToEitherAsync(future1, (result1) -> {
System.out.println("前面两个都执行完了执行的,返回结果1:" + result1);
return 3;
}, executor);
System.out.println(future3.get());
}
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(5);
CompletableFuture future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("线程1执行");
return "线程1";
}, executor);
CompletableFuture future2 = CompletableFuture.supplyAsync(() -> {
System.out.println("线程2执行");
return "线程2";
}, executor);
CompletableFuture future3 = CompletableFuture.supplyAsync(() -> {
System.out.println("线程3执行");
return "线程3";
}, executor);
/**
* allOf等待所有的Future执行完毕
* anyOf有一个执行完毕就会接着往下走
*/
CompletableFuture allOf = CompletableFuture.allOf(future1, future2, future3);
//得调用get方法进行阻塞
allOf.get();
CompletableFuture