java ExecutorService newSingleThreadExecutor 是否仅使用一个线程执行所有任务? [英] Is java ExecutorService newSingleThreadExecutor performs all the tasks using only one Thread?

查看:56
本文介绍了java ExecutorService newSingleThreadExecutor 是否仅使用一个线程执行所有任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道无法重新启动 java 线程.那么当我向newSingleThreadExecutor提交多个任务时,它是如何使用单线程执行所有任务的?

I know that a java thread cannot be restarted. So when I submit more than one tasks to newSingleThreadExecutor, then how does it perform all tasks using single thread?

我的理解是 newSingleThreadExecutor 一次最多使用一个线程来处理任何提交的任务.我猜 newFixedThreadPool 也是如此.如果一个线程不能重新启动,那么为了执行 n 个任务,应该产生 n 个线程.我认为 newSingleThreadExecutor、newFixedThreadPool 将确保不会同时产生很多线程,就像我们不使用 ExecutorService 那样(我们将每个任务附加到一个线程并单独启动)

My understanding is that newSingleThreadExecutor will use maximum one thread at a time to process any submitted tasks. I guess same for newFixedThreadPool. If a Thread cannot be restarted then for performing n tasks, n threads should be spawned. I think newSingleThreadExecutor, newFixedThreadPool will make sure that not many threads should be spawned at a same time, like we do without using ExecutorService (where we attach each task with a thread and start separately)

这是代码示例

class Task implements Runnable {
    public void run() {
        System.out.println("ThreadID-" + Thread.currentThread().getId());
        try {
            Thread.sleep(100);
        }
        catch (InterruptedException e) {
        }
    }
}

public class SingleThreadExecutorTest {
    public static void main(String[] args) {
        System.out.println("ThreadID-" + Thread.currentThread().getId());
        ExecutorService ex = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {
            ex.execute(new Task());
        }
    }
}

上面的代码总是打印相同的ThreadID.

The above code always prints the same ThreadID.

如果我替换下一行

Executors.newSingleThreadExecutor();

ExecutorService ex = Executors.newFixedThreadPool(2);

再一次,它能够使用 2 个线程执行所有任务.

Then again it is able to perform all tasks using 2 Threads.

仅当我使用

Executors.newCachedThreadPool();

我看到不同的线程 ID.

I see different Thread IDs.

ExecutorService 如何复用一个线程?不让它达到死亡状态吗?

How does ExecutorService reuse a Thread? Does it not let it reach to Dead State?

推荐答案

ThreadPoolExecutor 维护了一些 Worker 线程,它们的工作方式如下:

The ThreadPoolExecutor maintains some Worker threads, which work like this:

public class Demo {

    public class Worker implements Runnable {
        @Override
        public void run() {
            Runnable task = getTaskFromQueue();
            while (task != null) {
                task.run();
                task = getTaskFromQueue();  // This might get blocked if the queue is empty, so the worker thread will not terminate
            }
        }
    }

    public static void main(String[] args) {
        Worker worker = new Worker();
        Thread thread = new Thread(worker);
        thread.start();
    }

}

当您向具有单个 Worker 线程的 ThreadPoolExecutor 提交任务时,调用线程会将任务放入下面的 BlockingQueue条件:

When you submit a task to ThreadPoolExecutor which has a single Worker thread, the calling threads will put the task into a BlockingQueue on below condition:

  • 单个Worker很忙
  • BlockingQueue 未满

Worker空闲时,它会从这个BlockingQueue中检索新任务.

And when the Worker is free, it will retrieve new task from this BlockingQueue.

这篇关于java ExecutorService newSingleThreadExecutor 是否仅使用一个线程执行所有任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆