简体   繁体   English

Threadpool重用线程

[英]Threadpool re-use of threads

I know this topic has been asked a lot, but im not sure about one detail. 我知道这个话题已经问了很多,但是我不确定一个细节。 Now threadpool doesnt let a thread die after completing a task, and reuses it later on as needed (as it is said here , here , etc) But let say my runnable has variables in the constuctor - 现在,线程池犯规让完成任务后线程死亡,重用它以后需要(因为它是说在这里在这里 ,等),但让我说了可运行中的变量constuctor -

MyRunnable(int a){
  this.a = a; 
}

then, when we try to run the Runnable with a Executors.newFixedThreadPool (or something similar), we say 然后,当我们尝试使用Executors.newFixedThreadPool (或类似的东西)运行Runnable时,我们说

executor.execute(new MyRunnable(a)); // executor being Executors.newFixedThreadPool

now if variable 'a' is different in every execute, can Threadpool really reuse it later? 现在,如果变量“ a”在每个执行过程中都不相同,那么Threadpool以后是否可以真正重用它? I cant really understand how that would work, but i never seen 'Threadpool reuses threads except...', hence the confusion. 我无法真正理解它是如何工作的,但是我从未见过“ Threadpool重复使用线程,除了...”,因此造成了混乱。

No, neither the Runnable you submit, nor the variables related to it, will be reused. 不,您提交的Runnable以及与之相关的变量都不会被重用。

I think you mis-understood Thread and Runnable , they are different things. 我认为您误解了ThreadRunnable ,它们是不同的东西。 A Runnable is just normal object, execept its run method will be executed when you create a new thread with it. 一个Runnable只是普通的对象,除了它的run方法将在用它创建新线程时执行。 You can check this question . 您可以检查这个问题

The re-use of thread does not mean the re-use of Runnable , it means the thread keeps executing different Runnable s. 重用线程并不意味着重用Runnable ,这意味着线程将继续执行不同的Runnable


When you create a Thread with a Runnable , and start this thread like this: 当使用Runnable创建Thread时,像这样start该线程:

new Thread(new Runnable()).start()

the run() method of this Runnale will be executed, and after the run() exiting, this Thread will terminate too. Runnalerun()方法将被执行,并且在run()退出后,该Thread也将终止。

But, the Runnbale you submit to the ThreadPoolExecutor is not the one in code above to construct the thread. 但是,您提交给ThreadPoolExecutorRunnbale不是上面构建线程的代码中的那个。


Briefly, threads in ThreadPoolExecutor are created like this: 简要地说,在ThreadPoolExecutor中的ThreadPoolExecutor是这样创建的:

Runnable worker = new Runnable() {

    @Override
    public void run() {
        Runnable firstTask = getFirstTask();  // the first runnable 
        firstTask.run();

        Runnable queuedTask;
        while ( (queuedTask = getTaskFromQueue()) != null) {  // This could get blocked 
            queuedTask.run();
        }
    }
};
new Thread(worker).start();

Note, the Runnable used to initate the thread is not the one you submitted to the pool. 注意,用于初始化线程的Runnable不是您提交给池的线程。


When you submit new Runnable , the thread pool will check if it need to create new thread(based on the argument like corePoolSize ). 当您提交新的Runnable ,线程池将检查是否需要创建新线程(基于诸如corePoolSize类的参数)。

  • If it is necessary, then it create a new Worker with this Runnable as FirstTask , and create a new thread with this Worker and start it. 如果有必要,则使用Runnable作为FirstTask创建一个新的Worker ,并使用该Worker创建一个新线程并启动它。
  • If not, then it put the Runnbale in a queue. 如果不是,则将Runnbale队列。 When there are free threads, they will check this queue and take tasks from it. 当有空闲线程时,他们将检查此队列并从中获取任务。

So, from my point how the thread pool working algorithm would be similar and looks like below 因此,从我的角度来看,线程池的工作算法将如何类似并如下所示

while (check if the pool is not shutdown) {
    Runnable task = pool.fetchTaskFromQueue(); // fetch the Task from the queue. In your case it object of MyRunnable class
    task.run(); // call the run() of MyRunnable object
}

Thread pool resues the Thread , not the Runnable/ Callable implementation. 线程池继续使用Thread ,而不是Runnable/ Callable实现。 So, as per thread pool, it does reuse your variable a . 因此,根据线程池,它确实重用了variable a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM