• java基于quasar实现协程池【后篇】


    java基于quasar实现协程池【前篇】java基于quasar实现协程池_爪哇盘古的博客-CSDN博客

             在上一个文章中讲述了通过仿照java自写线程池的方式改写成quasar协程池,功能可以说实现了效果,但是遇到了一个烦恼就是在协程阻塞过程中会疯狂报警告,如果您的项目有日志文件产生当遇到一个非常耗时的任务时后面的任务阻塞产生警告,那么该日志文件的体量是致命的!所以为了摆脱这个问题,不要尝试、不要猜。要看文档!【我对英文很不友好的】

     quasar纤程文档FiberExecutorScheduler (Quasar 0.8.0)

     

    在该文档中我发现了FiberExecutorScheduler类,这个类将是本文阐述quasar协程池的正确打开方式!【全是泪】

    分析经历:我一直对Quasar及其轻质纤维替代Threads感到好奇。那么quasar本身是否有自己的纤程池呢?于是看是翻阅文档,找吧挨个看吧!

    线程池ThreadPoolExecutor类的实现。

    1. int maxThreadPoolSize = 10;
    2. ThreadPoolExecutor executor = new ThreadPoolExecutor(
    3. maxThreadPoolSize,
    4. maxThreadPoolSize,
    5. 10, TimeUnit.MINUTES,
    6. new ArrayBlockingQueue<Runnable>(maxThreadPoolSize),
    7. Executors.defaultThreadFactory(),
    8. new ThreadPoolExecutor.CallerRunsPolicy()
    9. );
    10. for (int i = 0; i < 100; i++) {
    11. executor.execute(new Runnable() {
    12. @Override
    13. public void run() {
    14. // run some code
    15. }
    16. });
    17. }

    上面的代码创建了一个具有10个线程的池,一个在池前的队列(该队列可以容纳10个元素)和一个拒绝策略(当队列已满时),以使主线程自己执行Runnable任务。当for循环创建100个可运行对象时,它们将在池中一次执行10个,排队等待10个,并且主线程自己拾取一个Runnable直到其他对象完成,然后主线程返回将Runnables添加到执行程序。

    每个光纤调度器调度的光纤,当您创建不带调度器的光纤时,将创建一个FiberForkJoinScheduler并将其分配给该光纤。

    简而言之,如果要管理线程池中的光纤,请使用FiberExecutorScheduler
    Quasar关于调度光纤的文档


    您的代码可能像这样

    1. //线程池任务数量
    2. int maxThreadPoolSize = 10;
    3. ThreadPoolExecutor executor = new ThreadPoolExecutor(
    4. maxThreadPoolSize,
    5. maxThreadPoolSize,
    6. 10, TimeUnit.MINUTES,
    7. new ArrayBlockingQueue<Runnable>(maxThreadPoolSize),
    8. Executors.defaultThreadFactory(),
    9. new ThreadPoolExecutor.CallerRunsPolicy()
    10. );
    11. //重点!通过FiberExecutorScheduler类将线程池传为协程池
    12. FiberExecutorScheduler scheduler = new FiberExecutorScheduler("FibersInAPool", executor);
    13. for (int i = 0; i < 100; i++) {
    14. int finalI = i;
    15. Fiber fiber = new Fiber<>(scheduler
    16. , new SuspendableRunnable() {
    17. @Override
    18. public void run() throws SuspendExecution, InterruptedException {
    19. // run some code
    20. System.out.println(finalI);
    21. Thread.sleep(1000);
    22. }
    23. });
    24. fiber.start();
    25. }

    这个操作看着可能会很奇怪,一个纤程池干嘛要利用线程池来加载使用?这可能与java底层本身不支持协程有关吧【猜】,不过通过这种形式也有好处就是可以直接通过开始的线程池的一些功能。方便了使用习惯(无非就是创建个线程池通过FiberExecutorScheduler来改为协程池进行协程操作),这样对于springboot的bean也会更友好的实现了吧!毕竟是通过线程池创建bean!

    以下是实现效果:

     

     不会出现阻塞警告了

     

     光纤非常便宜,因此您根本不需要池(及其异步作业调度模型):只需启动光纤,并在每次需要新的顺序进程与其他进程同时运行时让其运行常规顺序代码即可。

    当然它也支持类似go的管道【看文档】,可以自行开发你的业务逻辑。

    Channel (Quasar 0.8.0)

     

    1. package testgrp;
    2. import java.util.concurrent.ExecutionException;
    3. import co.paralleluniverse.strands.SuspendableCallable;
    4. import co.paralleluniverse.strands.SuspendableRunnable;
    5. import co.paralleluniverse.strands.channels.Channels;
    6. import co.paralleluniverse.strands.channels.IntChannel;
    7. import co.paralleluniverse.fibers.Fiber;
    8. import co.paralleluniverse.fibers.SuspendExecution;
    9. /**
    10. * Increasing-Echo Quasar Example
    11. *
    12. * @author circlespainter
    13. */
    14. public class QuasarIncreasingEchoApp {
    15. static public Integer doAll() throws ExecutionException, InterruptedException {
    16. final IntChannel increasingToEcho = Channels.newIntChannel(0); // Synchronizing channel (buffer = 0)
    17. final IntChannel echoToIncreasing = Channels.newIntChannel(0); // Synchronizing channel (buffer = 0)
    18. Fiber<Integer> increasing = new Fiber<>("INCREASER", new SuspendableCallable<Integer>() { @Override public Integer run() throws SuspendExecution, InterruptedException {
    19. // The following is enough to test instrumentation of synchronizing methods
    20. // synchronized(new Object()) {}
    21. int curr = 0;
    22. for (int i = 0; i < 10 ; i++) {
    23. Fiber.sleep(10);
    24. System.out.println("INCREASER sending: " + curr);
    25. increasingToEcho.send(curr);
    26. curr = echoToIncreasing.receive();
    27. System.out.println("INCREASER received: " + curr);
    28. curr++;
    29. System.out.println("INCREASER now: " + curr);
    30. }
    31. System.out.println("INCREASER closing channel and exiting");
    32. increasingToEcho.close();
    33. return curr;
    34. } }).start();
    35. Fiber<Void> echo = new Fiber<Void>("ECHO", new SuspendableRunnable() { @Override public void run() throws SuspendExecution, InterruptedException {
    36. Integer curr;
    37. while (true) {
    38. Fiber.sleep(1000);
    39. curr = increasingToEcho.receive();
    40. System.out.println("ECHO received: " + curr);
    41. if (curr != null) {
    42. System.out.println("ECHO sending: " + curr);
    43. echoToIncreasing.send(curr);
    44. } else {
    45. System.out.println("ECHO detected closed channel, closing and exiting");
    46. echoToIncreasing.close();
    47. return;
    48. }
    49. }
    50. } }).start();
    51. try {
    52. increasing.join();
    53. echo.join();
    54. } catch (ExecutionException e) {
    55. e.printStackTrace();
    56. } catch (InterruptedException e) {
    57. e.printStackTrace();
    58. }
    59. return increasing.get();
    60. }
    61. static public void main(String[] args) throws ExecutionException, InterruptedException {
    62. doAll();
    63. }
    64. }

    我们大java真是无所不能,通过这次实践也是让我倍感骄傲!以后谁也别说什么java不支持协程了~

  • 相关阅读:
    logstash/filebeat只接收最近一段时间的数据
    MySQL数据库的性能分析 ---图书《软件性能测试分析与调优实践之路》-手稿节选
    神经网络算法用什么语言,神经网络是一种算法吗
    数据结构-双向链表操作
    面试官:要不你来讲讲Spring 的IOC和AOP你是怎么理解的呗?
    nodejs如何删除指定文件夹的图片
    算法60天目录
    软件架构师考试的真实感受
    gin 集成 Swagger
    2、TCP协议基础
  • 原文地址:https://blog.csdn.net/weixin_47723549/article/details/125534140