java中的协程Quasar
2022/1/11 1:03:48
本文主要是介绍java中的协程Quasar,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
HELLOWORLD
public class Helloworld { @Suspendable static void m1() throws InterruptedException, SuspendExecution { String m = "m1"; //System.out.println("m1 begin"); m = m2(); //System.out.println("m1 end"); //System.out.println(m); } static String m2() throws SuspendExecution, InterruptedException { String m = m3(); Strand.sleep(1000); return m; } //or define in META-INF/suspendables @Suspendable static String m3() { List l = Stream.of(1,2,3).filter(i -> i%2 == 0).collect(Collectors.toList()); return l.toString(); } static public void main(String[] args) throws ExecutionException, InterruptedException { int count = 10000; testFiber(count); } static void testFiber(int count) throws InterruptedException { final CountDownLatch latch = new CountDownLatch(count); LongAdder latency = new LongAdder(); long t = System.currentTimeMillis(); for (int i =0; i< count; i++) { new Fiber<Void>("Caller", new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException { long start = System.currentTimeMillis(); m1(); start = System.currentTimeMillis() - start; latency.add(start); latch.countDown(); } }).start(); } latch.await(); t = System.currentTimeMillis() - t; long l = latency.longValue() / count; System.out.println("fiber took: " + t + ", latency: " + l + " ms"); } }
@Suspendable注解
这篇关于java中的协程Quasar的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现