怎样实现线程池?我在维基百科上看了相关信息,还是找不到答案(可能因为我没完全弄懂线程池的含义)
How would one implement a threadpool? I've been reading on wikipedia for "threadpool" but I still can't figure out what one should do to solve this question (possibly because I didn't quite understand what a threadpool is in simple terms).(原)
谁能给我简单解释一下什么是线程池吗?
Can someone explain me in plain english what a threadpool is and how would one answer this question?(原)
答1
线程池是一组预先实例化的线程,可随时执行运算任务。尤其当要执行大量短小运算任务时,使用线程池要优于为每个任务创建新线程,因为能防止大量创建线程而带来的开销。
A thread pool is a group of pre-instantiated, idle threads which stand ready to be given work. These are preferred over instantiating new threads for each task when there is a large number of short tasks to be done rather than a small number of long ones. This prevents having to incur the overhead of creating a thread a large number of times.(原)
具体实现据环境而不同,简单来说,你需要下面三步:
Implementation will vary by environment, but in simplified terms, you need the following:(原)
一种创建线程的方式,并设置其为空闲状态。可以通过类似互斥锁的机制将线程拦住,直到线程池分配任务。
一个存储线程的容器,类似队列的数据结构,把线程实例放进或取出。
一个标准接口或抽象类让线程执行任务。这种抽象类可能会叫Task,并且带有一个execute这样的执行方法。
A way to create threads and hold them in an idle state. This can be accomplished by having each thread wait at a barrier until the pool hands it work. (This could be done with mutexes as well.)A container to store the created threads, such as a queue or any other structure that has a way to add a thread to the pool and pull one out.A standard interface or abstract class for the threads to use in doing work. This might be an abstract class called Task with an execute() method that does the work and then returns.(原)
当线程池创建好,可以先创建固定量的线程等待,也可以按需来创建线程。
When the thread pool is created, it will either instantiate a certain number of threads to make available or create new ones as needed depending on the needs of the implementation.(原)
当线程池接收到执行任务,会从容器中取出线程来执行,并解开互斥锁。闲置线程就会恢复状态并执行运算任务。执行完毕后线程就被回收回池中,加上互斥锁设置为闲置状态等待下一个任务。
When the pool is handed a Task, it takes a thread from the container (or waits for one to become available if the container is empty), hands it a Task, and meets the barrier. This causes the idle thread to resume execution, invoking the execute() method of the Task it was given. Once execution is complete, the thread hands itself back to the pool to be put into the container for re-use and then meets its barrier, putting itself to sleep until the cycle repeats.(原)
25👍
“线程池是一组预先实例化的线程,可随时执行运算任务…它能能防止创建大量线程而带来的开销”。每当有人搜索“线程池”时,google就应该把这句话展示出来。 --Rafael EyngMay 30, 2015 at 14:56
👍
回答的简短到位。 --Blood-HaZaRdAug 1, 2018 at 10:44
答2
线程池是一个托管线程的集合,通常在一个队列中来执行另个任务队列中的任务。
Thread pool is a collection of managed threads usually organized in a queue, which execute the tasks in the task queue.(原)
当你想执行运算时,每次创建一个线程对象是一笔不小的开销。而在线程池中,你只需要把运算任务加到任务队列中,线程池就会分配线程来依次执行队列中的任务。
Creating a new thread object every time you need something to be executed asynchronously is expensive. In a thread pool you would just add the tasks you wish to be executed asynchronously to the task queue and the thread pool takes care of assigning an available thread, if any, for the corresponding task. As soon as the task is completed, the the now available thread requests another task (assuming there is any left).(原)
线程池可以帮助你避免过度创建或销毁线程。
Thread pool helps you avoid creating or destroying more threads, than would really be necessary.(原)
如果要实现线程池,我会创建一个带有线程队列的类和一个任务队列,然后实现一个添加任务的方法。此外,你还需设置线程池的最大容量。
I would start by creating a class with a queue of threads and a queue of tasks. Then implement a method which adds a task to the task queue and move on from there. Obviously, you should also make it possible to set the maximum allowed threads in a thread pool.(原)
引用:
[1] John Smith: https://softwareengineering.stackexchange.com/users/42742/john-smith
[2] Blrfl: https://softwareengineering.stackexchange.com/users/20756/blrfl
[3] Dante: https://softwareengineering.stackexchange.com/users/43990/dante