Skip to content

Commit d381976

Browse files
author
biezhi
committed
📦 update completablefuture video url
1 parent 0031740 commit d381976

File tree

12 files changed

+322
-1
lines changed

12 files changed

+322
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Java8 改变了我们思考和编码的方式,在这里你可以学习到 Java
3131
|第 10 课 | [Stream API(下)](https://github.com/biezhi/learn-java8/blob/master/java8-stream/README.md#collector-%E6%94%B6%E9%9B%86) | [网易云课堂](http://study.163.com/course/courseLearn.htm?courseId=1005047049&utm_campaign=commission&utm_source=cp-400000000397038&utm_medium=share#/learn/video?lessonId=1051571684&courseId=1005047049) ¦ [哔哩哔哩](https://www.bilibili.com/video/av19287893/index_10.html#page=10) ¦ [Youtube](https://youtu.be/iubE0ezu-xI) | [stream](https://github.com/biezhi/learn-java8/tree/master/java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson3) |
3232
|第 11 课 | [新的日期时间 API](https://github.com/biezhi/learn-java8/blob/master/java8-datetime-api/README.md) | [网易云课堂](http://study.163.com/course/courseLearn.htm?courseId=1005047049&utm_campaign=commission&utm_source=cp-400000000397038&utm_medium=share#/learn/video?lessonId=1051571688&courseId=1005047049) ¦ [哔哩哔哩](https://www.bilibili.com/video/av19287893/index_11.html#page=11) ¦ [Youtube](https://youtu.be/hKXJvh-id1E) | [datetime](https://github.com/biezhi/learn-java8/tree/master/java8-datetime-api/src/main/java/io/github/biezhi/datetime) |
3333
|第 12 课 | [并发增强](https://github.com/biezhi/learn-java8/blob/master/java8-concurrent/README.md) | [网易云课堂](http://study.163.com/course/courseLearn.htm?courseId=1005047049&utm_campaign=commission&utm_source=cp-400000000397038&utm_medium=share#/learn/video?lessonId=1051682806&courseId=1005047049) ¦ [哔哩哔哩](https://www.bilibili.com/video/av19287893/index_12.html#page=12) ¦ [Youtube](https://youtu.be/OYkToWIDEEI) | [concurrent](https://github.com/biezhi/learn-java8/tree/master/java8-concurrent/src/main/java/io/github/biezhi/java8/concurrent) |
34-
|第 13 课 | CompletableFuture | 网易云课堂 ¦ 哔哩哔哩 ¦ Youtube | |
34+
|第 13 课 | [CompletableFuture](https://github.com/biezhi/learn-java8/blob/master/java8-completablefuture/README.md) | [网易云课堂](http://study.163.com/course/courseLearn.htm?courseId=1005047049&utm_campaign=commission&utm_source=cp-400000000397038&utm_medium=share#/learn/video?lessonId=1051908792&courseId=1005047049) ¦ [哔哩哔哩](https://www.bilibili.com/video/av19287893/index_13.html#page=13) ¦ [Youtube](https://youtu.be/4reRygD1dGo) | [completablefuture](https://github.com/biezhi/learn-java8/tree/master/java8-completablefuture/src/main/java/io/github/biezhi/java8/completablefuture) |
3535
|第 14 课 | Nashorn 引擎 | 网易云课堂 ¦ 哔哩哔哩 ¦ Youtube | |
3636
|第 15 课| Java 8 最佳实践 | 网易云课堂 ¦ 哔哩哔哩 ¦ Youtube | |
3737
|第 16 课| 函数式编程的正确姿势 | 网易云课堂 ¦ 哔哩哔哩 ¦ Youtube | |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
5+
/**
6+
* 创建 CompletableFuture
7+
*
8+
* @author biezhi
9+
* @date 2018/3/25
10+
*/
11+
public class CompletableFuture1 {
12+
13+
public static void main(String[] args) {
14+
CompletableFuture<Void> helloFuture = CompletableFuture.runAsync(() -> System.out.println("hello future"));
15+
16+
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> 2333);
17+
18+
}
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* @author biezhi
9+
* @date 2018/3/25
10+
*/
11+
public class CompletableFuture10 {
12+
13+
private static CompletableFuture<Integer> m1(){
14+
return CompletableFuture.supplyAsync(() -> {
15+
try {
16+
TimeUnit.SECONDS.sleep(1);
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
}
20+
return 2333;
21+
});
22+
}
23+
private static CompletableFuture<Integer> m2(){
24+
return CompletableFuture.supplyAsync(() -> {
25+
try {
26+
TimeUnit.SECONDS.sleep(2);
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
return 8877;
31+
});
32+
}
33+
34+
public static void main(String[] args) throws ExecutionException, InterruptedException {
35+
long start = System.currentTimeMillis();
36+
CompletableFuture.anyOf(m1(), m2())
37+
.thenRun(() -> {
38+
System.out.println(System.currentTimeMillis() - start);
39+
}).get()
40+
;
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* 处理计算结果
9+
*
10+
* @author biezhi
11+
* @date 2018/3/25
12+
*/
13+
public class CompletableFuture2 {
14+
15+
public static void main(String[] args) {
16+
CompletableFuture<Integer> uCompletableFuture = CompletableFuture.supplyAsync(() -> {
17+
System.out.println("开始执行运算");
18+
try {
19+
TimeUnit.SECONDS.sleep(3);
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
int a = 1/0;
24+
System.out.println("执行结束");
25+
return 2333;
26+
});
27+
28+
try {
29+
Integer result = uCompletableFuture.whenComplete((a, b) -> {
30+
System.out.println("Result: " + a);
31+
System.out.println("Exception: " + b);
32+
}).exceptionally(e -> {
33+
System.out.println(e.getMessage());
34+
return 666;
35+
}).get();
36+
System.out.println(result);
37+
} catch (InterruptedException e) {
38+
e.printStackTrace();
39+
} catch (ExecutionException e) {
40+
e.printStackTrace();
41+
}
42+
}
43+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.function.BiFunction;
7+
8+
/**
9+
* 结果转换
10+
*
11+
* @author biezhi
12+
* @date 2018/3/25
13+
*/
14+
public class CompletableFuture3 {
15+
16+
public static void main(String[] args) {
17+
try {
18+
String result = CompletableFuture.supplyAsync(() -> 2333)
19+
.thenApply(String::valueOf).get();
20+
System.out.println(result);
21+
} catch (InterruptedException | ExecutionException e) {
22+
e.printStackTrace();
23+
}
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
6+
/**
7+
* 扁平转换
8+
*
9+
* @author biezhi
10+
* @date 2018/3/25
11+
*/
12+
public class CompletableFuture4 {
13+
14+
public static void main(String[] args) {
15+
try {
16+
String s = CompletableFuture.supplyAsync(() -> 23333)
17+
.thenCompose(t -> CompletableFuture.supplyAsync(() -> t + "ddd"))
18+
.get();
19+
System.out.println(s);
20+
} catch (InterruptedException | ExecutionException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
6+
/**
7+
* 消费结果
8+
*
9+
* @author biezhi
10+
* @date 2018/3/25
11+
*/
12+
public class CompletableFuture5 {
13+
14+
public static void main(String[] args) throws ExecutionException, InterruptedException {
15+
CompletableFuture.supplyAsync(() -> 9999)
16+
.thenAccept(System.out::println).get();
17+
}
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
6+
/**
7+
* @author biezhi
8+
* @date 2018/3/25
9+
*/
10+
public class CompletableFuture6 {
11+
12+
public static void main(String[] args) throws ExecutionException, InterruptedException {
13+
CompletableFuture.supplyAsync(() -> 9999)
14+
.thenAcceptBoth(CompletableFuture.supplyAsync(() -> "7878"), (a, b) -> {
15+
System.out.println("a = " + a);
16+
System.out.println("b = " + b);
17+
}).get();
18+
}
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* @author biezhi
9+
* @date 2018/3/25
10+
*/
11+
public class CompletableFuture7 {
12+
13+
public static void main(String[] args) throws ExecutionException, InterruptedException {
14+
CompletableFuture.supplyAsync(() -> {
15+
try {
16+
System.out.println("开始执行了");
17+
TimeUnit.SECONDS.sleep(3);
18+
} catch (InterruptedException e) {
19+
e.printStackTrace();
20+
}
21+
return 9999;
22+
}).thenRun(() -> {
23+
System.out.println("执行结束了");
24+
}).get();
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.biezhi.java8.completablefuture;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
6+
/**
7+
* @author biezhi
8+
* @date 2018/3/25
9+
*/
10+
public class CompletableFuture8 {
11+
12+
public static void main(String[] args) {
13+
try {
14+
String s = CompletableFuture.supplyAsync(() -> 23333)
15+
.thenCombine(CompletableFuture.supplyAsync( () -> "8898" ), (a, b) -> {
16+
System.out.println("a =" + a);
17+
System.out.println("b =" + b);
18+
return a + b;
19+
})
20+
.get();
21+
System.out.println(s);
22+
} catch (InterruptedException e) {
23+
e.printStackTrace();
24+
} catch (ExecutionException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)