...
Thread Pools overcome these disadvantages as the maximum number of worker threads that can be initiated and executed simultaneously, can be controlled. Every worker accepts a Runnable
from a request and stores it in a temporary Channel
like a buffer or a queue until resources become available. Since Because threads are reused and can be efficiently added to the Channel
, most of the thread creation overhead is eliminated.
...
Wiki Markup |
---|
This compliant solution uses a _Fixed Thread Pool_ that places an upper bound on the number of simultaneously executing threads. Tasks submitted to the pool are stored in an internal queue. This prevents Thethe system willfrom notgetting getoverwhelmed overwhelmedwhen trying to respond to all incoming requests and butallows it willto degrade gracefully by serving a fixed number of clients at a particular time. \[[Tutorials 08|AA. Java References#Tutorials 08]\] |
Wiki Markup |
---|
According to the Java API \[[API 06|AA. Java References#API 06]\] the interface {{java.util.concurrent}} Interface {{.Executor}}: |
Wiki Markup \[The Interface {{Executor}} is\] An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An {{Executor}} is normally used instead of explicitly creating threads.
...
In reality, there are some gotchas problems associated with the usage of the Executor
interface. For one, a task that depends on other tasks should not execute in the same Thread Pool. A task that submits another task to a single threaded Executor
remains blocked until the results are received whereas the second task waits until the first one has concluded. This constitutes a deadlock.
...
Always try to submit independent tasks to the Executor
. Choosing a large pool size can also help reduce thread starvation problems. Note that any operation that has further constraints, such as the total number of database connections or total ResultSets
open at a particular time impose an upper bound on the Thread Pool size since as each thread would continue blocking continues to block until the resource becomes available. The other rules of fair concurrency, such as not running response sensitive tasks, also apply.
...