Java1.8项目纤程实战和性能压测

开发 前端
当你的线程池,异步出现性能问题时,请考虑纤程,让你的代码性能数量级的提升,线程池之所以慢,是因为大量的线程频繁的上下文切换,和线程此中任务争夺线程while循环,耗cpu那是相当多,纤程就避免了这个问题。

新建一个maven依赖项目。

pom.xml中引入纤程jar的坐标依赖。

<dependency>
   <groupId>co.paralleluniverse</groupId>
   <artifactId>quasar-core</artifactId>
   <version>0.7.4</version>
   <classifier>jdk8</classifier>
</dependency>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

纤程使用示例。

package com.example.demo.fiber;
import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.futures.AsyncCompletionStage;
import co.paralleluniverse.strands.Strand;
import co.paralleluniverse.strands.SuspendableRunnable;
import com.example.demo.fiber.tool.WorkTools;
import java.util.concurrent.CountDownLatch;
/**
 * 描述:纤程demo </br>
 * 作者:王林冲 </br>
 * 时间:2023/4/7 15:44
 */
public class Test {
    public static void main(String[] args) throws InterruptedException {
        fiberTest();
    }
    public static void fiberTest() throws InterruptedException {
        new Fiber(() -> {
            Strand.sleep(5000);
            System.out.println("纤程开始执行了");
        }).start();
        System.out.println("主线程执行完毕");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

为了实现类似于线程池的功能,想在一个批量处理的过程中,开多个纤程处理,在统一获取结果,然后继续主线程执行,场景相当多。

自己实现个纤程池。

package com.example.demo.fiber.tool;
import co.paralleluniverse.fibers.Fiber;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
 * 描述:协程工作程池 </br>
 * 作者:王林冲 </br>
 * 时间:2023/4/7 17:33
 */
@Slf4j
public class FiberWorkPool {
    /**
     * 工作协程数组
     */
    private List<Fiber> workThreads;
    /**
     * 协程任务倒计数门栓
     */
    private CountDownLatch countDownLatch = new CountDownLatch(0);
    /**
     * 建立协程池,taskCount 为协程池中工做协程的个数
     * @param taskCount
     */
    public FiberWorkPool(int taskCount) {
        workThreads = new ArrayList<>(taskCount);
        countDownLatch = new CountDownLatch(taskCount);
    }
    /**
     * 任务加入任务队列
     * @param task
     */
    public void execute(Fiber task) {
        try {
            workThreads.add(task);   //阻塞接口的Fiber work插入
        } catch (Exception e) {
            log.error("========> Fiber work add failed ..., msg : {}", e.getMessage());
        }
    }
    //销毁协程池,该方法保证全部任务都完成的状况下才销毁全部协程,不然等待任务完成再销毁
    public void shutdown() throws InterruptedException {
        start();
        countDownLatch.await();
        workThreads.clear();  //清空等待队列
        log.debug("========> successfully closed FiberWorkPool ...");
    }
    /**
     * 启动协程池里所有的协程
     */
    public void start() {
        if (workThreads.size() != 0) {
            for (Fiber fiber : workThreads) {
                fiber.start();
            }
        }
    }
    /**
     * 获取倒计数门栓
     *
     * @return
     */
    public CountDownLatch getCountDownLatch() {
        return this.countDownLatch;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.

纤程池使用demo:

package com.example.demo.fiber.tool;
import co.paralleluniverse.fibers.Fiber;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.*;
/**
 * 描述:协程池应用demo </br>
 * 作者:王林冲 </br>
 * 时间:2023/4/10 17:17
 */
@Component
public class FiberWorkPoolAppDemo {
    public void fiber() throws InterruptedException {
        //开启5个协程,50个任务列队。
        FiberWorkPool fiberWorkPool = new FiberWorkPool(50);
        for (int i = 0; i < 50; i++) {
            fiberWorkPool.execute(new Fiber(() -> {
                Fiber.sleep(50);
                //System.out.println("========= " + Fiber.currentFiber().getName() + " ============");
                fiberWorkPool.getCountDownLatch().countDown();
            }));
        }
        //等待协程任务完毕后再结束主线程
        fiberWorkPool.shutdown();
    }
    public void thread() throws ExecutionException, InterruptedException {
        List<Future<Void>> futures = Lists.newArrayList();
        ExecutorService executorService = Executors.newFixedThreadPool(50);
        for (int i = 0; i < 50; i++) {
            futures.add(executorService.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    Thread.sleep(50);
                    return null;
                }
            }));
        }
        for (Future<Void> future : futures) {
            future.get();
        }
        executorService.shutdownNow();
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.

测试controller:

package com.example.demo.fiber.tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ExecutionException;
/**
 * 描述:协程controller </br>
 * 作者:王林冲 </br>
 * 时间:2023/4/10 17:20
 */
@RestController
@RequestMapping("/fiber")
public class FiberTestController {
    @Autowired
    private FiberWorkPoolAppDemo fiberWorkPoolAppDemo;
    /**
     * 协程测试
     * @throws InterruptedException
     * @throws ExecutionException
     */
    @GetMapping("/fiberTest")
    public void fiberTest () throws InterruptedException, ExecutionException {
        Long start = System.currentTimeMillis();
        fiberWorkPoolAppDemo.fiber();
        System.out.println("=======> "+ (System.currentTimeMillis() - start) + "=======毫秒");
    }
    /**
     * 线程测试
     * @throws InterruptedException
     * @throws ExecutionException
     */
    @GetMapping("/threadTest")
    public void threadTest () throws InterruptedException, ExecutionException {
        Long start = System.currentTimeMillis();
        fiberWorkPoolAppDemo.thread();
        System.out.println("=======> "+ (System.currentTimeMillis() - start) + "=======毫秒");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

jmeter压测参数。

图片

线程池压测。

图片

纤程池压测​。

图片

差距一目了然,当你的线程池,异步出现性能问题时,请考虑纤程,让你的代码性能数量级的提升,线程池之所以慢,是因为大量的线程频繁的上下文切换,和线程此中任务争夺线程while循环,耗cpu那是相当多,纤程就避免了这个问题。所以性能刚杠杠的。

责任编辑:姜华 来源: IT学习道场
相关推荐

2023-11-17 11:36:59

协程纤程操作系统

2023-01-03 10:30:00

Java工具

2023-02-22 08:15:13

压测模拟计算

2021-12-03 08:45:57

RocketMQ压测性能

2009-06-01 11:09:16

OSGI实战进阶

2023-10-12 09:46:00

并发模型线程

2010-11-08 13:04:36

SQL SERVER线

2010-11-08 13:15:13

SQL SERVER线

2023-10-11 07:39:58

Java虚拟线程

2023-01-05 11:44:43

性能HTTPS

2010-07-08 16:00:51

SQL Server线

2018-01-10 14:08:34

阿里双11压测

2009-10-14 15:01:45

VB6.0项目升级

2010-07-07 14:57:25

SQL Server线

2019-08-19 00:14:12

网络测试带宽网络流量

2022-11-25 18:49:11

云原生

2009-12-18 09:17:29

VS 2008项目

2023-10-31 18:12:03

压测工具测试

2020-06-12 16:10:58

进程线程Java

2009-12-21 17:53:45

Fedora Core
点赞
收藏

51CTO技术栈公众号