【问题标题】:Can a thread run another fiber when the running fiber is in blocked当正在运行的光纤处于阻塞状态时,线程是否可以运行另一条光纤
【发布时间】:2016-12-01 02:47:19
【问题描述】:

据我所知,当正在运行的光纤被阻塞时,一个线程可以运行另一个光纤。但事实并非如此。我创建了100个光纤,它将搜索solr。我发现的结果是所有光纤都在执行order。只有当前一个纤程像线程一样完成时,才能执行另一个纤程。这是我的代码。

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.FiberForkJoinScheduler;
import co.paralleluniverse.fibers.FiberScheduler;
import co.paralleluniverse.fibers.SuspendExecution;

public class FilterThreadTest {
    static FiberForkJoinScheduler fiberForkJoinScheduler = new FiberForkJoinScheduler("fork-join-schedule", 1);
    static SolrService solrService = new SolrService();

    public static void main(String[] args) {
        solrService.init();
        for (int i = 0; i < 100; i++) {
            new CountFiber(fiberForkJoinScheduler, i, solrService).start();
        }
        try {
            Thread.sleep(10000000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

class CountFiber extends Fiber<Void> {

    /**
     * 
    */
    private static final long serialVersionUID = 1L;
    private int count;
    private SolrService solrService;

    public CountFiber(FiberScheduler scheduler, int count, SolrService     solrService) {
        super(scheduler);
        // TODO Auto-generated constructor stub
        this.count = count;
        this.solrService = solrService;
    }

    @Override
    public Void run() throws SuspendExecution, InterruptedException {
        System.out.println(count + " fiber is starting!");
        solrService.search();
        System.out.println(count + " fiber is ended!");
        return null;
    }

}

我对光纤有误解吗?

【问题讨论】:

    标签: fiber quasar


    【解决方案1】:

    只有在执行 fiber-blocking 调用而不是 thread-blocking 并且 Quasar 不会自动转换 thread-将调用阻塞到光纤阻塞调用中,因此您需要为不了解 Quasar 的现有工具编写(通常是小型的)集成。

    Quasar 提供的并发编程库(类似 Go 的通道、类似 Erlang 的actor、数据流编程、反应流和java.util.concurrent 端口)支持光纤阻塞(当从光纤调用时)和线程阻塞(当从线程调用); Comsat 集成也是如此,它涵盖了许多工具,但截至目前,还没有 Solr。您是自己构建了 Solr 集成还是 solrService.search() 只是线程阻塞?

    有关将工具与 Quasar 集成的更多信息(通常很容易),请参阅例如 this blog post

    【讨论】:

      猜你喜欢
      • 2012-07-26
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 2018-10-10
      • 2020-05-03
      相关资源
      最近更新 更多