据我所知,当正在运行的纤程阻塞时,一个线程可以运行另一个纤程。但事实并非如此。我创建了100个纤程,它将搜索solr。我发现的结果是所有纤程都按顺序执行。另一个纤维只有在前一个像线程一样完成时才能执行。这是我的代码。
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;
}
}
我误解了纤维吗?