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

新建一个maven依赖项目

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

<dependency>
   <groupId>co.paralleluniverse</groupId>
   <artifactId>quasar-core</artifactId>
   <version>0.7.4</version>
   <classifier>jdk8</classifier>
</dependency>

纤程使用示例

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("主线程执行完毕");
    }

}

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

自己实现个纤程池

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;
    }


}

纤程池使用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();
    }


}

测试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) + "=======毫秒");
    }

}

jmeter压测参数

 线程池压测

纤程池压测

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT学习道场

为你的进步加点油!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值