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-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南