java协程_什么是协程?Java中如何支持?

协程(Coroutine):是单线程下的并发,又称微线程,纤程。简单理解就是线程中的线程。

优点:

轻量,创建成本小,降低了内存消耗

用户态调度,减少了 CPU 上下文切换的开销,提高了 CPU 缓存命中率

减少同步加锁,提高了性能

可以用同步思维写异步代码

缺点:

在协程执行中不能有阻塞操作,否则整个线程被阻塞

不擅长处理 CPU 密集型

适用场景:

高性能要求,考虑牺牲公平性换取吞吐量

IO 密集型任务

Generator 式的流式计算

Java 官方目前是还没推出协程

目前可用性比较高的有 Quasar 和 ea-async 两个第三方库,都是通过 byte code Instrument,把编译后同步程序class文件修改为异步的操作。

1、Quasar:

https://github.com/puniverse/quasar

提供了Fiber实现,调度器,甚至Channel,Actor编程范式这样的支持

尝试

package testgrp;

import java.util.concurrent.ExecutionException;

import co.paralleluniverse.fibers.Fiber;

import co.paralleluniverse.fibers.SuspendExecution;

import co.paralleluniverse.strands.SuspendableRunnable;public classTestFiber {void startFiber(intnum) throws ExecutionException, InterruptedException {for (int i = 0; i < num; i++) {

finalint index =i;

Fiber fiber = new Fiber("fiber", newSuspendableRunnable() {

@Overridepublic voidrun() throws SuspendExecution, InterruptedException {

System.out.println("fiber-" +index);

Fiber.sleep(60000);

}

});

fiber.start();

}

Thread.sleep(60000);

}

}

package testgrp;

import org.junit.Test;public classTestFiberAndThread {

@Testpublic voidtest() throws Exception {new TestFiber().startFiber(10000);//new TestThread().startThread(10000);

}

}

2、ea-async:

https://github.com/electronicarts/ea-async

通过 Instrument 代码,提供 async-await 风格协程实现的工具

使用ea-async编写的代码,方法必须返回 CompletableFuture 或 CompletionStage

尝试

pom依赖

com.ea.async

ea-async

1.2.3

测试代码:

package constxiong.interview;

importstatic com.ea.async.Async.await;

importstaticjava.util.concurrent.CompletableFuture.completedFuture;

import java.util.concurrent.CompletableFuture;public classTestEaAsync {public static voidmain(String[] args) {

String result=test().join();

System.out.println(result);

System.out.println(testAsync());

}public static CompletableFuturetest() {return CompletableFuture.supplyAsync(() ->{try{

Thread.sleep(1000);

}catch(InterruptedException e) {

e.printStackTrace();

}return "Hello";

}).thenCombine(CompletableFuture.supplyAsync(()->{try{

Thread.sleep(2000);

}catch(InterruptedException e) {

e.printStackTrace();

}return "ConstXiong";

}), (s1, s2)->{return s1 + " " +s2;

});

}public staticString testAsync() {

CompletableFuture cf1 = CompletableFuture.supplyAsync(() ->{try{

Thread.sleep(1000);

}catch(InterruptedException e) {

e.printStackTrace();

}return "Hello";

});

CompletableFuture cf2 = CompletableFuture.supplyAsync(() ->{try{

Thread.sleep(2000);

}catch(InterruptedException e) {

e.printStackTrace();

}return "ConstXiong";

});return await(cf1) + " " +await(cf2);

}

}

参考:

1cca7dbe01f4ad912bf189fc2922ad07.png

所有资源资源汇总于公众号

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值