java异步非阻塞_java高级开发必学!java高并发系统之异步非阻塞

同步阻塞调用

即串行调用,响应时间为所有服务的响应时间总和;

public class Test {

public static void main(String[] args) throws Exception {

RpcService rpcService = new RpcService();

HttpService httpService = new HttpService();

//耗时10ms

Map result1 = rpcService.getRpcResult();

//耗时20ms

Integer result2 = httpService.getHttpResult();

//总耗时30ms

}

static class RpcService {

Map getRpcResult() throws Exception {

//调用远程方法(远程方法耗时约10ms,可以使用Thread.sleep模拟)

Thread.sleep(10);

return null;

}

}

static class HttpService {

Integer getHttpResult() throws Exception {

//调用远程方法(远程方法耗时约20ms,可以使用Thread.sleep模拟)

Thread.sleep(20);

return 0;

}

}

}

半异步(异步Future)

线程池,异步Future,使用场景:并发请求多服务,总耗时为最长响应时间;提升总响应时间,但是阻塞主请求线程,高并发时依然会造成线程数过多,CPU上下文切换;

简单说一下 Future模式,Future模式可以这样来描述:我有一个任务,提交给了Future,Future替我完成这个任务。期间我自己可以去做任何想做的事情。一段时间之后,我就便可以从Future那儿取出结果。

public interface Future {

boolean cancel(boolean mayInterruptIfRunning);

boolean isCancelled();

boolean isDone();

V get() throws InterruptedException, ExecutionException;

V get(long timeout, TimeUnit unit)

throws InterruptedException, ExecutionException, TimeoutException;

}

public class Test {

final static ExecutorService executor = Executors.newFixedThreadPool(2);

public static void main(String[] args) {

RpcService rpcService = new RpcService();

HttpService httpService = new HttpService();

Future> future1 = null;

Future future2 = null;

try {

//lambda表达式写法 java1.8的写法

future1 = executor.submit(() -> rpcService.getRpcResult());

future2 = executor.submit(() -> httpService.getHttpResult());

//这里可以执行其他逻辑任务

//耗时10ms ,300为超时时间

Map result1 = future1.get(300, TimeUnit.MILLISECONDS);

//耗时20ms

Integer result2 = future2.get(300, TimeUnit.MILLISECONDS);

//总耗时20ms

} catch (Exception e) {

if (future1 != null) {

future1.cancel(true);

}

if (future2 != null) {

future2.cancel(true);

}

throw new RuntimeException(e);

}

}

static class RpcService {

Map getRpcResult() throws Exception {

//调用远程方法(远程方法耗时约10ms,可以使用Thread.sleep模拟)

}

}

static class HttpService {

Integer getHttpResult() throws Exception {

//调用远程方法(远程方法耗时约20ms,可以使用Thread.sleep模拟)

}

}

}

注意:强烈建议自己读一下 java.util.concurrent - Java并发工具包 ,这里后续有时间会整理一篇博客

全异步(Callback)

Callback方式调用,使用场景:不考虑回调时间且只能对结果做简单处理(直接写在Future的回调方法中),如果依赖服务是两个或两个以上服务,则不能合并两个服务的处理结果;不阻塞主请求线程,但使用场景有限。

异步回调链式编排

异步回调链式编排(JDK8 CompletableFuture),使用场景:其实不是异步调用方式,只是对依赖多服务的Callback调用结果处理做结果编排,来弥补Callback的不足,从而实现全异步链式调用。下面贴几个简单的使用例子。

//模拟一个任务执行完执行另一个任务

public static void test() throws Exception {

CompletableFuture completableFuture1=CompletableFuture.supplyAsync(()->{

//模拟执行耗时任务

System.out.println("task1 doing...");

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

//返回结果

return "result1";

});

//等第一个任务完成后,将任务结果传给参数result,执行后面的任务并返回一个代表任务的completableFuture

CompletableFuture completableFuture2= completableFuture1.thenCompose(result->CompletableFuture.supplyAsync(()->{

//模拟执行耗时任务

System.out.println("task2 doing...");

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

//返回结果

return "result2";

}));

System.out.println(completableFuture2.get());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值