java并发

This commit is contained in:
罗祥
2019-11-29 17:58:46 +08:00
parent 3f371f110d
commit 538f89ebc9
14 changed files with 913 additions and 114 deletions

View File

@ -0,0 +1,22 @@
package com.heibaiying.future;
import java.util.concurrent.*;
public class J0_Callable {
static class Task implements Callable<Integer> {
@Override
public Integer call() throws InterruptedException {
Thread.sleep(3000);
return 100;
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executors = Executors.newSingleThreadExecutor();
Future<Integer> future = executors.submit(new Task());
System.out.println("计算结果为:" + future.get());
executors.shutdown();
}
}

View File

@ -1,43 +0,0 @@
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();
}
}

View File

@ -0,0 +1,26 @@
package com.heibaiying.future;
import java.util.concurrent.*;
public class J1_FutureTask {
static class Task implements Callable<Integer> {
@Override
public Integer call() throws InterruptedException {
Thread.sleep(3000);
return 100;
}
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> futureTask01 = new FutureTask<>(new Task());
FutureTask<Integer> futureTask02 = new FutureTask<>(new Task());
new Thread(futureTask01).start();
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(futureTask02);
System.out.println("futureTask01 计算结果为:" + futureTask01.get());
System.out.println("futureTask02 计算结果为:" + futureTask01.get());
executorService.shutdown();
}
}

View File

@ -26,15 +26,16 @@ public class J2_CompletableFuture {
}
public static void main(String[] args) throws InterruptedException {
int intermediateResult;
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;
}
System.out.println("启动主线程");
Thread.sleep(2000);
System.out.println("主线程计算完成");
future.complete(i);
// 假设主线程计算结果为 100
intermediateResult = 100;
// 传递主线程的计算结果给子线程
future.complete(intermediateResult);
}
}

View File

@ -6,17 +6,13 @@ 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;
return 100;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {

View File

@ -38,6 +38,6 @@ public class J4_StreamingCall {
.thenApply(J4_StreamingCall::multi)
.thenAccept(J4_StreamingCall::accept) //值在这一步被消费掉了
.thenAccept(x -> System.out.println("运算结果:" + x));
future.get(); //惰性求值,如果缺少这一步,不会有任何输出
future.get(); //类似于流式计算的惰性求值,如果缺少这一步,不会有任何输出
}
}