0

As far as I see,a thread can run another fiber when the running fiber is in blocked.But it is not the case.I create 100 fibers which will search solr.The result I find is all the fibers is executed in order.Another fiber can execute only if the previous one is finished just like a thread.This is my code.

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

}

Did I misunderstand fiber?

1 Answers1

0

Fibers will yield execution to other non-blocked fibers only when they perform fiber-blocking calls, not thread-blocking ones and Quasar doesn't automatically transform thread-blocking calls into fiber-blocking ones so you need to write (small, usually) integrations for pre-existing tools that don't know about Quasar.

The concurrent programming libraries provided by Quasar (Go-like channels, Erlang-like actors, dataflow programming, reactive streams and the java.util.concurrent port) support both fiber-blocking (when called from fibers) and thread-blocking (when called from threads); the same is true for Comsat integrations that cover many tools but, as of today, not Solr. Did you build a Solr integration yourself or is solrService.search() only thread-blocking?

For more information about integrating tools with Quasar (it's usually quite easy) see for example this blog post.

circlespainter
  • 836
  • 5
  • 8