Threadpool如何重用Threads及其工作原理 [英] How Threadpool re-use Threads and how it works

查看:193
本文介绍了Threadpool如何重用Threads及其工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的多线程概念很弱并且试图学习。



在我所知道的java中,我们不能再调用一次线程即

 线程t =新线程(//一些Runnable); 
t.start()

t.start()//非法并在运行时抛出异常。

据我所知,当你再次调用t.start()时会抛出异常因为关联线程的堆栈一旦退出 run()方法就被销毁,你试图再次初始化。



<在那种情况下,我对Threadpool的了解是,它提供了更好的性能和优势。节省时间,因为不需要创建新线程。 (我在 http://www.javatpoint.com/thread-pooling-in-java上阅读



如果不需要在ThreadPool场景中创建新线程,那么它如何与刚刚完成其run方法的同一线程一起工作,那将是Thread可以再次使用吗?



我读到了这个, http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html
并且它说大多数执行者实施java.util.concurrent使用线程池,它由工作线程组成。这种线程与它执行的Runnable和Callable任务分开存在,通常用于执行多个任务。



那么这里的Worker线程是什么,它与普通的Java线程有什么不同?



这个链接缓存线程池如何重用现有线程我得到了一些东西,但仍然对于什么样的东西可以消除时感到困惑我们使用Thread Pool,然后使用普通的Java Thread提供更好的性能。



所以我们可以这样说,



Thread有三个部分,


  1. 创建(告诉操作系统它是新线程,为它创建堆栈。)

  2. 执行Runnable即run()方法。

  3. 销毁线程。

因此,考虑到以上3个步骤,使用Threadpool步骤1和步骤3可以在固定数量的线程创建后消除。只执行每个任务的第2步,这就是为什么Threadpool更快?我们可以这样说吗?我是否正确?

解决方案


如果不需要在ThreadPool场景中创建新线程,那么如何使用刚刚完成其run方法的相同线程,该Thread是否可以再次使用?


简单 - 原始线程从不实际完成。它只是等待执行另一个任务。在伪代码中:

  //不,这甚至不够准确!一般印象只有:) 
while(!pool.isShutdown()){
Runnable task = pool.waitForTaskOnQueue();
task.run();
}

(显然当线程池关闭时,它需要停止等待等待另一个任务的线程 - 但希望你得到一般的想法。)


My Multithreading concepts are weak and trying to learn.

In java what i know is, we can't call a Thread more then once i.e

Thread t = new Thread(//Some Runnable);
t.start()

t.start() //Illegal and throw Exception at Runtime.

As far as I know, it throws exception when you call t.start() again because the associated stack for the Thread is destroyed once it goes out of run() method and you are trying to initialize things again.

In that case, what I know about Threadpool is, it gives Better performance & saves time because there is no need to create new thread. (I read in http://www.javatpoint.com/thread-pooling-in-java)

if there is no need to create new Thread in ThreadPool scenario, then how it works with same thread which just finished its run method, will that Thread can be used again?

I read this, http://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html and it says that "Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks."

So what is Worker thread here, is it something different then normal Java Threads?

with this link How Does a Cached Thread Pool Reuse Existing Threads I got something but still confused on what kind of stuff can be eliminated when we use Thread Pool and which gives better performance then using normal Java Thread.

So can we say like this,

Thread has three parts,

  1. Creation (Telling OS that it is new Thread, create stack for it.)
  2. Execute Runnable ie run() method.
  3. Destroying Threads.

So, considering above 3 steps, With Threadpool step 1 and Step 3 can be eliminated after fixed number of Thread Creation. only Step 2 for each task will be executed that is why Threadpool is faster? can we say like this? am I correct?

解决方案

If there is no need to create new Thread in ThreadPool scenario, then how it works with same thread which just finished its run method, will that Thread can be used again?

Simple - the original thread never actually completes. It just waits for another task to execute. In pseudo-code:

// No, this isn't even slightly accurate! General impression only :)
while (!pool.isShutdown()) {
    Runnable task = pool.waitForTaskOnQueue();
    task.run();
}

(Obviously when a thread pool is shut down, it would need to stop waiting threads from waiting for another task, too - but hopefully you get the general idea.)

这篇关于Threadpool如何重用Threads及其工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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