请输入您要查询的百科知识:

 

词条 Thread pool
释义

  1. Performance

  2. See also

  3. References

  4. External links

In computer programming, a thread pool is a software design pattern for achieving concurrency of execution in a computer program. Often also called a replicated workers or worker-crew model,[1] a thread pool maintains multiple threads waiting for tasks to be allocated for concurrent execution by the supervising program. By maintaining a pool of threads, the model increases performance and avoids latency in execution due to frequent creation and destruction of threads for short-lived tasks.[2] The number of available threads is tuned to the computing resources available to the program, such as parallel processors, cores, memory, and network sockets.[3]

A common method of scheduling tasks for thread execution is a synchronized queue, known as a task queue. The threads in the pool remove waiting tasks from the queue, and place them into a completed task queue after completion of execution.

Performance

The size of a thread pool is the number of threads kept in reserve for executing tasks. It is usually a tunable parameter of the application, adjusted to optimize program performance.

The primary benefit of a thread pool over creating a new thread for each task is that thread creation and destruction overhead is restricted to the initial creation of the pool, which may result in better performance and better system stability. Creating and destroying a thread and its associated resources is an expensive process in terms of time. An excessive number of threads in reserve, however, wastes memory, and context-switching between the runnable threads invokes performance penalties. A socket connection to another network host, which might take many CPU cycles to drop and re-establish, can be maintained more efficiently by associating it with a thread that lives over the course of more than one network transaction.

Typically, a thread pool executes on a single computer. However, thread pools are conceptually related to server farms in which a master process, which might be a thread pool itself, distributes tasks to worker processes on different computers, in order to increase the overall throughput. Embarrassingly parallel problems are highly amenable to this approach.{{cn|date=December 2016}}

The number of threads may be dynamically adjusted during the lifetime of an application based on the number of waiting tasks. For example, a web server can add threads if numerous web page requests come in and can remove threads when those requests taper down.{{disputed inline|reason=This sounds more like pre-spawning than like a pool pattern.|date=December 2015}} The cost of having a larger thread pool is increased resource usage. The algorithm used to determine when to create or destroy threads affects the overall performance:

  • Creating too many threads wastes resources and costs time creating the unused threads.
  • Destroying too many threads requires more time later when creating them again.
  • Creating threads too slowly might result in poor client performance (long wait times).
  • Destroying threads too slowly may starve other processes of resources.

See also

  • Object pool pattern
  • Concurrency pattern
  • Grand Central Dispatch
  • Parallel Extensions
  • Parallelization
  • Server farm
  • Staged event-driven architecture

References

1. ^Garg, Rajat P. & Sharapov, Ilya Techniques for Optimizing Applications - High Performance Computing Prentice-Hall 2002, p. 394
2. ^{{Cite book |first=Allen |last=Holub |authorlink=Allen Holub |title=Taming Java Threads |publisher=Apress |year=2000 |page=209}}
3. ^{{cite book |title=TCP/IP Sockets in C#: Practical Guide for Programmers |first1=David B. |last1=Makofske |first2=Michael J. |last2=Donahoo |first3=Kenneth L. |last3=Calvert |publisher=Academic Press |year=2004 |page=112}}

}}

External links

  • "[https://web.archive.org/web/20080207124322/http://today.java.net/pub/a/today/2008/01/31/query-by-slice-parallel-execute-join-thread-pool-pattern.html Query by Slice, Parallel Execute, and Join: A Thread Pool Pattern in Java]" by Binildas C. A.
  • "Thread pools and work queues" by Brian Goetz
  • "[https://www.codeproject.com/Articles/3631/A-Method-of-Worker-Thread-Pooling A Method of Worker Thread Pooling]" by Pradeep Kumar Sahu
  • "[https://www.codeproject.com/Articles/3607/Work-Queue Work Queue]" by Uri Twig: C++ code demonstration of pooled threads executing a work queue.
  • "Windows Thread Pooling and Execution Chaining"
  • "Smart Thread Pool" by Ami Bar
  • "Programming the Thread Pool in the .NET Framework" by David Carmona
  • "The Thread Pool and Asynchronous Methods" by Jon Skeet
  • "Creating a Notifying Blocking Thread Pool in Java" by Amir Kirsh
  • "Practical Threaded Programming with Python: Thread Pools and Queues" by Noah Gift
  • "Optimizing Thread-Pool Strategies for Real-Time CORBA" by Irfan Pyarali, Marina Spivak, Douglas C. Schmidt and Ron Cytron
  • Conference Paper "Deferred cancellation. A behavioral pattern" by Philipp Bachmann
{{Design Patterns patterns}}

4 : Threads (computing)|Software design patterns|Concurrent computing|Parallel computing

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/9/25 14:25:32