Fiber

代码实现

Talk is cheap, show you the code.

package com.mjy.ds.juc.ds_fiber;

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.strands.SuspendableRunnable;
import com.google.common.collect.Lists;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import lombok.Data;
import org.springframework.util.StopWatch;

public class HelloFiber {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        final List<Rule> ruleList = initRuleList();
        int detailSize = 10_0000;
        List<Detail> detailList = initDetailList(detailSize);

        StopWatch stopwatch = new StopWatch();
        stopwatch.start("NORMAL");
        matchNormal(ruleList, detailList);
        stopwatch.stop();

        detailList = initDetailList(detailSize);
        stopwatch.start("Thread");
        matchThread(ruleList, detailSize, detailList);
        stopwatch.stop();

        detailList = initDetailList(detailSize);
        stopwatch.start("Fiber");
        matchFiber(ruleList, detailList);
        stopwatch.stop();

        System.err.println(stopwatch.prettyPrint());

    }

    private static void matchThread(List<Rule> ruleList, int detailSize, List<Detail> detailList)
            throws InterruptedException {
        Thread[] threads = new Thread[detailSize];
        for (int i = 0; i < detailSize; i++) {
            Detail detail = detailList.get(i);
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {
                    matchOneRule(ruleList, detail);
                }
            });
        }
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
    }

    private static void matchFiber(List<Rule> ruleList, List<Detail> detailList)
            throws ExecutionException, InterruptedException {
        Fiber<Void>[] fibers = new Fiber[detailList.size()];
        for (int i = 0; i < fibers.length; i++) {
            Detail detail = detailList.get(i);
            fibers[i] = new Fiber<Void>(new SuspendableRunnable() {
                public void run() throws SuspendExecution, InterruptedException {
                    matchOneRule(ruleList, detail);
                }
            });
        }
        for (Fiber<Void> fiber : fibers) {
            fiber.start();
        }
        for (Fiber<Void> fiber : fibers) {
            fiber.join();
        }
    }

    private static void matchNormal(List<Rule> ruleList, List<Detail> detailList) {
        for (int i = 0; i < detailList.size(); i++) {
            matchOneRule(ruleList, detailList.get(i));
        }
    }

    private static void matchOneRule(List<Rule> ruleList, Detail detail) {
        boolean flag;
        for (int j = 0; j < ruleList.size(); j++) {
            Rule rule = ruleList.get(j);
            flag = false;
            if (Objects.equals(detail.getRule1(), rule.getRule1())) {
                flag = true;
            }
            if (!flag || !Objects.equals(detail.getRule2(), rule.getRule2())) {
                continue;
            }
            if (!flag || !Objects.equals(detail.getRule3(), rule.getRule3())) {
                continue;
            }
            if (!flag || !Objects.equals(detail.getRule4(), rule.getRule4())) {
                continue;
            }
            if (flag) {
                detail.setClassification(rule.getClassification());
                break;
            }
        }
    }

    private static List<Detail> initDetailList(int detailSize) {
        List<Detail> detailList = Lists.newArrayList();
        for (int i = 0; i < detailSize; i++) {
            Detail detail = new Detail();
            detail.setRule1("rule_" + ((Double) (Math.random() * 5)).intValue());
            detail.setRule2("rule_" + ((Double) (Math.random() * 5)).intValue());
            detail.setRule3("rule_" + ((Double) (Math.random() * 5)).intValue());
            detail.setRule4("rule_" + ((Double) (Math.random() * 6)).intValue());
            detailList.add(detail);
        }
        return detailList;
    }

    private static List<Rule> initRuleList() {
        List<Rule> ruleList = Lists.newArrayList();
        for (int j = 0; j < 5; j++) {
            for (int i = 0; i < 5; i++) {
                for (int k = 0; k < 5; k++) {
                    for (int l = 0; l < 5; l++) {
                        Rule rule = new Rule();
                        rule.setRule1("rule_" + i);
                        rule.setRule2("rule_" + j);
                        rule.setRule3("rule_" + k);
                        rule.setRule4("rule_" + l);
                        rule.setClassification("classification_" + i + j + k + l);
                        ruleList.add(rule);
                    }
                }
            }
        }
        return ruleList;
    }


    @Data
    public static class Rule {

        private String rule1;

        private String rule2;

        private String rule3;

        private String rule4;

        private String classification;
    }

    @Data
    public static class Detail {

        private String rule1;

        private String rule2;

        private String rule3;

        private String rule4;

        private String classification;

    }

}


运行结果

StopWatch '': running time = 7054414574 ns
---------------------------------------------
ns         %     Task name
---------------------------------------------
366181923  005%  NORMAL
5944870525  084%  Thread
743362126  011%  Fiber

纤程的应用场景

纤程 vs 线程池:很短的计算任务,不需要和内核打交道,并发量高!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值