This commit is contained in:
xiangwb
2018-02-07 17:20:56 +08:00
parent f98860aa25
commit 1b43d1378c
2 changed files with 44 additions and 32 deletions
@@ -1,6 +1,8 @@
package com.xwbing.demo;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
@@ -16,36 +18,46 @@ import java.util.concurrent.ExecutionException;
* 参数: Function(函数)有返回值 Consumer(消费者)无返回值
*/
public class CompletableFutureDemo {
private static final Logger logger = LoggerFactory.getLogger(CompletableFutureDemo.class);
/**
* 结合两个CompletionStage的结果,进行转化后返回
*
* @return 有返回值
*/
public static JSONObject thenCombine() {
try {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}).thenCombine(CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "world";
}), (s1, s2) -> {
JSONObject object = new JSONObject();
object.put("s1", s1);
object.put("s2", s2);
return object;
}).get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("获取数据异常");
}
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "hello";
}).thenCombine(CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//出现异常
if (1 == 1)
throw new RuntimeException("异常");
return "world";
}), (s1, s2) -> {
JSONObject object = new JSONObject();
object.put("s1", s1);
object.put("s2", s2);
return object;
}).exceptionally(e -> {
throw new RuntimeException(e.getMessage());
}).join();
}
public static void main(String[] args) {
long l = System.currentTimeMillis();
thenCombine();
long l1 = System.currentTimeMillis();
System.out.println(l1 - l);
}
/**
@@ -75,12 +87,14 @@ public class CompletableFutureDemo {
jsonObject.put("s2", s2);
}).get();
} catch (InterruptedException | ExecutionException e) {
logger.error(e.getMessage());
throw new RuntimeException("获取数据异常");
}
return jsonObject;
}
/**
* 在两个CompletionStage都运行完执行
* 不关心这两个的结果,只关心这两个执行完毕,之后在进行操作(Runnable)
*
* @return void
@@ -150,11 +164,4 @@ public class CompletableFutureDemo {
}
return object;
}
public static void main(String[] args) {
long l = System.currentTimeMillis();
acceptEither();
long l1 = System.currentTimeMillis();
System.out.println(l1 - l);
}
}
@@ -2,6 +2,8 @@ package com.xwbing.demo;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import javax.annotation.Resource;
@@ -19,6 +21,7 @@ import java.util.stream.Collectors;
public class LambdaDemo {
@Resource
private static ThreadPoolTaskExecutor taskExecutor;
private static final Logger logger = LoggerFactory.getLogger(LambdaDemo.class);
public static void main(String[] args) {
//匿名内部类
@@ -86,7 +89,8 @@ public class LambdaDemo {
try {
List<JSONObject> sysUsers = future.get();
} catch (InterruptedException | ExecutionException e) {
throw new BusinessException("");
logger.error(e.getMessage());
throw new BusinessException("获取数据出错");
}
}
@@ -111,6 +115,7 @@ public class LambdaDemo {
try {
completableFuture.get();
} catch (InterruptedException | ExecutionException e) {
logger.error(e.getMessage());
throw new BusinessException("获取数据出错");
}
return finalList;