利用CompletableFuture集齐7龙珠
世界上有7颗龙珠,传说集齐7颗龙珠就会实现任意一个愿望,
这7可龙珠分散在世界各地,而你在中国武汉,需要将龙珠搜集在一起,
请编写一段程序来表明你的收集方案
分析
- 没有限定收集龙珠的人数,所以我可以动用7个人同时搜集龙珠~
- 从出发到搜集龙珠,可以当成一个方法
- 最后将集齐的7颗龙珠放在一起召唤神龙,许愿
编码
发布收集龙珠的任务
class SevenDragonBall{
boolean b1;
boolean b2;
boolean b3;
boolean b4;
boolean b5;
boolean b6;
boolean b7;
}
SevenDragonBall sevenDragonBall = new SevenDragonBall();
召集7个人手
final List<Runnable> runnables = Arrays.asList(
runnable1,
runnable2,
runnable3,
runnable4,
runnable5,
runnable6,
runnable7
);
每个人都有搜集各自龙珠的任务
Runnable runnable1 = ()->collectB1(sevenDragonBall);
Runnable runnable2 = ()->collectB2(sevenDragonBall);
Runnable runnable3 = ()->collectB3(sevenDragonBall);
Runnable runnable4 = ()->collectB4(sevenDragonBall);
Runnable runnable5 = ()->collectB5(sevenDragonBall);
Runnable runnable6 = ()->collectB6(sevenDragonBall);
Runnable runnable7 = ()->collectB7(sevenDragonBall);
开始任务,等待所有人收集到龙珠
runnables.stream()
.map((Function<Runnable, CompletableFuture<Void>>) CompletableFuture::runAsync)
.collect(Collectors.toList())
.stream()
.map(CompletableFuture::join)
.forEach(v->{});
确定收集到龙珠后,召唤神龙许愿
Assertions.assertTrue(sevenDragonBall.b1);
Assertions.assertTrue(sevenDragonBall.b2);
Assertions.assertTrue(sevenDragonBall.b3);
Assertions.assertTrue(sevenDragonBall.b4);
Assertions.assertTrue(sevenDragonBall.b5);
Assertions.assertTrue(sevenDragonBall.b6);
Assertions.assertTrue(sevenDragonBall.b7);
makeAwish();
完整代码
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;
public class CompletableFutureTest {
/**
* 世界上有7颗龙珠,传说集齐7颗龙珠就会实现任意一个愿望,
* 这7可龙珠分散在世界各地,而你在中国武汉,需要将龙珠搜集在一起,
* 请编写一段程序来表明你的收集方案
*/
@Test
public void testFindDragonBall(){
final long s1 = System.currentTimeMillis();
SevenDragonBall sevenDragonBall = new SevenDragonBall();
Runnable runnable1 = ()->collectB1(sevenDragonBall);
Runnable runnable2 = ()->collectB2(sevenDragonBall);
Runnable runnable3 = ()->collectB3(sevenDragonBall);
Runnable runnable4 = ()->collectB4(sevenDragonBall);
Runnable runnable5 = ()->collectB5(sevenDragonBall);
Runnable runnable6 = ()->collectB6(sevenDragonBall);
Runnable runnable7 = ()->collectB7(sevenDragonBall);
final List<Runnable> runnables = Arrays.asList(
runnable1,
runnable2,
runnable3,
runnable4,
runnable5,
runnable6,
runnable7
);
runnables.stream()
.map((Function<Runnable, CompletableFuture<Void>>) CompletableFuture::runAsync)
.collect(Collectors.toList())
.stream()
.map(CompletableFuture::join)
.forEach(v->{});
final long s2 = System.currentTimeMillis();
Assertions.assertTrue(sevenDragonBall.b1);
Assertions.assertTrue(sevenDragonBall.b2);
Assertions.assertTrue(sevenDragonBall.b3);
Assertions.assertTrue(sevenDragonBall.b4);
Assertions.assertTrue(sevenDragonBall.b5);
Assertions.assertTrue(sevenDragonBall.b6);
Assertions.assertTrue(sevenDragonBall.b7);
//许愿
makeAwish();
Assertions.assertTrue(s2-s1 <= 1000+2000+3000+4000+5000+6000+7000);
}
private void makeAwish() {
System.out.println("中二病也要谈恋爱");
}
private void collect(long s) {
try {
Thread.sleep(s);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void collectB1(SevenDragonBall sevenDragonBall){
collect(1000);
sevenDragonBall.b1 = true;
}
private void collectB2(SevenDragonBall sevenDragonBall){
collect(2000);
sevenDragonBall.b2 = true;
}
private void collectB3(SevenDragonBall sevenDragonBall){
collect(3000);
sevenDragonBall.b3 = true;
}
private void collectB4(SevenDragonBall sevenDragonBall){
collect(4000);
sevenDragonBall.b4 = true;
}
private void collectB5(SevenDragonBall sevenDragonBall){
collect(5000);
sevenDragonBall.b5 = true;
}
private void collectB6(SevenDragonBall sevenDragonBall){
collect(6000);
sevenDragonBall.b6 = true;
}
private void collectB7(SevenDragonBall sevenDragonBall){
collect(7000);
sevenDragonBall.b7 = true;
}
static class SevenDragonBall{
boolean b1;
boolean b2;
boolean b3;
boolean b4;
boolean b5;
boolean b6;
boolean b7;
}
}