我使用 ThreadPoolExecutor() 为我的应用程序运行多个线程。我想用单线程进行测试,所以在这种情况下我设置 nb_threads = 1。但我不确定它是否正确,所以你能帮我只取一个线程吗?
这是我的代码部分:
private ThreadPoolExecutor executor = null;
public static int NB_THREADS_MAX = 8;
public void submit(Runnable inRunnable) {
if (executor == null) {
/*Choice exactly the number of threads that relates the number of available processors*/
nb_threads = NB_THREADS_MAX < (tmp = Runtime.getRuntime().availableProcessors())
? NB_THREADS_MAX
: tmp;
executor = new ThreadPoolExecutor(nb_threads, nb_threads, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); /*In this case, the pool size is fixed*/
}
executor.submit(inRunnable);
}
Run Code Online (Sandbox Code Playgroud)