您可以编写自己的ThreadFactory
ThreadFactory custom = new CustomThreadFactory();
ExecutorService executorService = Executors.newCachedThreadPool(custom); //or use newSingleThreadExecutor(..)
Scheduler customScheduler = Schedulers.from(executorService); 现在
你可以使用这个调度,并需要一个新的线程时不阻塞主队列以及获取调用:
class CustomThreadFactory implements ThreadFactory {
public Thread lastT;
public int newThreadCounter = 0;
@Override
public Thread newThread(Runnable r) {
newThreadCounter++;
System.out.println("newThread called");
Thread lastT = new Thread(r); //or CustomThread(r)
return lastT;
}
}
,你甚至可以去进一步和仪器新线程 -
class CustomThread extends Thread {
public CustomThread(Runnable r) {
super(r);
}
@Override
public void run() {
System.out.printf("About to run!");
super.run();
}
}
}