Skip to content

Commit 6ad56ea

Browse files
feat
1 parent af104ce commit 6ad56ea

File tree

10 files changed

+132
-1
lines changed

10 files changed

+132
-1
lines changed

Future/Future.iml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,24 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
<orderEntry type="module-library">
21+
<library>
22+
<CLASSES>
23+
<root url="jar://$MAVEN_REPOSITORY$/org/projectlombok/lombok/1.18.20/lombok-1.18.20.jar!/" />
24+
</CLASSES>
25+
<JAVADOC />
26+
<SOURCES />
27+
</library>
28+
</orderEntry>
1029
</component>
1130
</module>

Future/src/CompletableFutureUsage.java renamed to Future/src/completable/CompletableFutureUsage01.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
package completable;
2+
13
import java.util.concurrent.*;
24

35
/**
46
* @Author: zzStar
57
* @Date: 09-07-2021 22:23
68
*/
7-
public class CompletableFutureUsage {
9+
public class CompletableFutureUsage01 {
810

911
public static void main(String[] args) throws ExecutionException, InterruptedException {
1012
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 20, 1L, TimeUnit.MINUTES,
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package completable;
2+
3+
import org.junit.Test;
4+
5+
import java.util.StringJoiner;
6+
import java.util.concurrent.CompletableFuture;
7+
8+
/**
9+
* @Author: zzStar
10+
* @Date: 09-11-2021 11:38
11+
*/
12+
public class CompletableFutureUsage02 {
13+
14+
public static void sleepSecond(long second) {
15+
try {
16+
Thread.sleep(second * 1000);
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
}
21+
22+
public static void printMessage(String tag) {
23+
String res = new StringJoiner("\t|\t")
24+
.add(String.valueOf(System.currentTimeMillis()))
25+
.add(String.valueOf(Thread.currentThread().getId()))
26+
.add(Thread.currentThread().getName())
27+
.add(tag).toString();
28+
System.out.println(res);
29+
}
30+
31+
@Test
32+
public void supplyAsync() {
33+
// supplyAsync开启
34+
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
35+
printMessage("First");
36+
sleepSecond(2);
37+
printMessage("Second");
38+
sleepSecond(1);
39+
return "done";
40+
});
41+
printMessage("Play");
42+
printMessage(future.join());
43+
}
44+
45+
@Test
46+
public void thenCombine() {
47+
// thenCombine合并
48+
CompletableFuture<String> combine = CompletableFuture.supplyAsync(() -> {
49+
printMessage("1");
50+
sleepSecond(1);
51+
return "1";
52+
}).thenCombine(CompletableFuture.supplyAsync(() -> {
53+
printMessage("2");
54+
sleepSecond(2);
55+
return "2";
56+
}), (a, b) -> {
57+
System.out.println("a = " + a);
58+
System.out.println("b = " + b);
59+
return String.format("%s + %s", a, b);
60+
});
61+
printMessage("3");
62+
printMessage(combine.join());
63+
}
64+
65+
@Test
66+
public void thenApply() {
67+
// 相当于stream().map()
68+
CompletableFuture<String> apply = CompletableFuture.supplyAsync(() -> {
69+
printMessage("over");
70+
sleepSecond(1);
71+
return "1";
72+
// thenApplyAsync会将后面的代码块当作一个独立的任务并且创建新的线程执行
73+
}).thenApply(res -> {
74+
printMessage(res + " -> res");
75+
sleepSecond(2);
76+
return "2";
77+
});
78+
printMessage("3");
79+
printMessage(apply.join());
80+
}
81+
82+
@Test
83+
public void applyToEither() {
84+
CompletableFuture<String> apply = CompletableFuture.supplyAsync(() -> {
85+
printMessage("1");
86+
sleepSecond(3);
87+
return "1";
88+
}).applyToEither(CompletableFuture.supplyAsync(() -> {
89+
printMessage("2");
90+
sleepSecond(2);
91+
return "2";
92+
}), res -> res);
93+
printMessage(apply.join());
94+
}
95+
96+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package future;
2+
13
/**
24
* @Author: zzStar
35
* @Date: 10-13-2020 09:34
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package future;
2+
13
import java.util.concurrent.*;
24

35
/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package future;
2+
13
import java.util.concurrent.*;
24

35
/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package future;
2+
13
import java.util.ArrayList;
24
import java.util.Random;
35
import java.util.concurrent.*;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package future;
2+
13
import java.util.Random;
24
import java.util.concurrent.*;
35

Future/src/RunnableCantThrowsException.java renamed to Future/src/future/RunnableCantThrowsException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package future;
2+
13
/**
24
* 在run方法中无法抛出checked Exception
35
* <p>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package future;
2+
13
import java.util.concurrent.*;
24

35
/**

0 commit comments

Comments
 (0)