多线程编程
This commit is contained in:
@ -0,0 +1,43 @@
|
||||
package com.heibaiying.future;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class J1_Future {
|
||||
|
||||
static class Task implements Callable<Integer> {
|
||||
|
||||
private int operator;
|
||||
|
||||
Task(Integer operator) {
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
Thread.sleep(500);
|
||||
return operator * 10;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ExecutorService executors = Executors.newFixedThreadPool(20);
|
||||
List<Future<Integer>> futureList = new ArrayList<>();
|
||||
for (int i = 0; i <= 100; i++) {
|
||||
Future<Integer> submit = executors.submit(new Task(i));
|
||||
futureList.add(submit);
|
||||
}
|
||||
// 获取所有线程的返回值并计算
|
||||
Integer reduce = futureList.stream().map(x -> {
|
||||
try {
|
||||
return x.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 0;
|
||||
}).reduce(0, Integer::sum);
|
||||
System.out.println("计算结果为:" + reduce);
|
||||
executors.shutdown();
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.heibaiying.future;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class J2_CompletableFuture {
|
||||
|
||||
static class Compute implements Runnable {
|
||||
|
||||
private CompletableFuture<Integer> future;
|
||||
|
||||
Compute(CompletableFuture<Integer> future) {
|
||||
this.future = future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
System.out.println("子线程等待主线程运算完成····");
|
||||
Integer integer = future.get();
|
||||
System.out.println("子线程完成后续运算:" + integer * integer);
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
CompletableFuture<Integer> future = new CompletableFuture<>();
|
||||
System.out.println("主线程开始计算");
|
||||
new Thread(new Compute(future)).start();
|
||||
int i = 0;
|
||||
for (int j = 0; j < 100; j++) {
|
||||
i = i + j;
|
||||
}
|
||||
Thread.sleep(2000);
|
||||
System.out.println("主线程计算完成");
|
||||
future.complete(i);
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.heibaiying.future;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class J3_SupplyAsync {
|
||||
|
||||
private static Integer compute() {
|
||||
int i = 0;
|
||||
for (int j = 0; j < 100; j++) {
|
||||
i = i + j;
|
||||
}
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("子线程计算完成");
|
||||
return i;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(J3_SupplyAsync::compute);
|
||||
System.out.println("主线程等待子线程计算完成");
|
||||
Integer integer = supplyAsync.get();
|
||||
System.out.println("主线程计算完成:" + integer * integer);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.heibaiying.future;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 流式调用
|
||||
*/
|
||||
public class J4_StreamingCall {
|
||||
|
||||
private static Integer compute() {
|
||||
System.out.println("compute所在线程:" + Thread.currentThread().getId());
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return 100;
|
||||
}
|
||||
|
||||
private static Integer multi(Integer integer) {
|
||||
try {
|
||||
System.out.println("multi所在线程:" + Thread.currentThread().getId());
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return integer * integer;
|
||||
}
|
||||
|
||||
private static void accept(Integer integer) {
|
||||
System.out.println("accept所在线程:" + Thread.currentThread().getId());
|
||||
System.out.println("accept方法消费掉计算结果:" + integer);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<Void> future = CompletableFuture.supplyAsync(J4_StreamingCall::compute)
|
||||
.thenApply(J4_StreamingCall::multi)
|
||||
.thenAccept(J4_StreamingCall::accept) //值在这一步被消费掉了
|
||||
.thenAccept(x -> System.out.println("运算结果:" + x));
|
||||
future.get(); //惰性求值,如果缺少这一步,不会有任何输出
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.heibaiying.future;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 异常捕获
|
||||
*/
|
||||
public class J5_AbnormalCapture {
|
||||
|
||||
private static Integer compute() {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
int i = 100 / 0;
|
||||
return 100;
|
||||
}
|
||||
|
||||
private static Integer dealException(Throwable e) {
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
CompletableFuture<Void> future = CompletableFuture.supplyAsync(J5_AbnormalCapture::compute)
|
||||
.exceptionally(J5_AbnormalCapture::dealException)
|
||||
.thenAccept(System.out::println);
|
||||
future.get();
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.heibaiying.future;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 利用 thenCompose 和 thenCombineAsync 组合多个 CompletableFuture
|
||||
*/
|
||||
public class J6_Combination {
|
||||
|
||||
private static Integer compute() {
|
||||
System.out.println("compute 所在线程:" + Thread.currentThread().getId());
|
||||
return 100;
|
||||
}
|
||||
|
||||
private static Integer multi(Integer integer) {
|
||||
System.out.println("epr 所在线程:" + Thread.currentThread().getId());
|
||||
return integer * integer;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
// 组合实现方式1 thenCompose 一个计算的输入依赖另外一个计算的结果
|
||||
CompletableFuture<Void> future01 = CompletableFuture.supplyAsync(J6_Combination::compute)
|
||||
.thenCompose(x -> CompletableFuture.supplyAsync(() -> multi(x)))
|
||||
.thenAccept(x -> System.out.println("运算结果:" + x));
|
||||
future01.get();
|
||||
|
||||
System.out.println();
|
||||
|
||||
// 组合实现方式2 thenCombineAsync 两个计算之间不依赖
|
||||
CompletableFuture<Integer> future02 = CompletableFuture.supplyAsync(J6_Combination::compute);
|
||||
CompletableFuture<Integer> future03 = CompletableFuture.supplyAsync(() -> J6_Combination.multi(100));
|
||||
CompletableFuture<Integer> futureAll = future02.thenCombineAsync(future03, (x, y) -> x + y);
|
||||
System.out.println("运算结果:" + futureAll.get());
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user