A thread pool is a form of multithreading in which tasks are added to a queue and then automatically started after the thread is created. Thread pool threads are background threads. Each thread uses the default stack size, runs at the default priority, and is in a multithreaded unit. If a thread is idle in managed code (such as waiting for an event), the thread pool inserts another worker thread to keep all the processors busy. If all thread pool threads are always busy, but the queue contains pending work, the thread pool will create another worker thread after a period of time but the number of threads will never exceed the maximum value. Threads exceeding the maximum can be queued, but they will not start until the other thread finishes.
Part
1. Thread pool Manager (Threadpoolmanager): for creating and managing thread pools
2. Worker threads (workthread): Thread pool threads
3. Task interfaces (tasks): the interfaces that each task must implement for the work thread to schedule tasks to execute.
4. Task queues: For storing tasks that are not handled. Provides a buffer mechanism.
Technical background editing
In object-oriented programming, it is time-consuming to create and destroy objects because creating an object takes up memory resources or other additional resources. This is especially true in Java, where the virtual machine will attempt to track each object so that it can be garbage collected after the object is destroyed. So one way to improve the efficiency of the service program is to minimize the number of objects created and destroyed, especially the creation and destruction of resource-consuming objects. How to use the existing objects to serve is a key problem to be solved, in fact, this is the reason why some "pooling resources" technology arises. For example, everyone familiar with the database connection pool is based on this idea, this article will introduce the thread pool technology is also consistent with this idea.
At present, some well-known large companies are particularly optimistic about the technology, and has long been in their products to apply the technology. For example, IBM's Websphere,iona Orbix 2000 in Sun Jini, Microsoft's MTS (Microsoft Transaction Server 2.0), COM + and so on.
4 feature Editors
Applications can have multiple threads, which take a lot of time to wait for events to occur in hibernation. Other threads may go to sleep and only periodically wake up to change or update state information and then go to sleep again. To simplify the management of these threads,. NET Framework provides a thread pool for each process, a thread pool has several wait operation states, and when a wait operation completes, the worker thread in the thread pool executes the callback function. Threads in the thread pool are managed by the system, and programmers do not have to struggle with thread management to concentrate on the application tasks.
Application scope Edit 1, requires a large number of threads to complete the task, and the time to complete the task is relatively short. The Web server completes the task of Web page requests, and it is appropriate to use thread pooling technology. Because a single task is small and the number of tasks is huge, you can imagine the number of hits on a popular site. But for a long time task, such as a Telnet connection request, the advantage of the thread pool is not obvious. Because the Telnet session time is larger than the thread creation time. 2, the performance of demanding applications, such as requiring the server to respond quickly to customer requests. 3, accept a large number of sudden requests, but not so that the server generated a large number of thread applications. Sudden large number of customer requests, in the absence of a thread pool, will produce a large number of threads, although theoretically most of the maximum number of operating system threads is not a problem, a short period of time to produce a large number of threads may bring memory to the limit, and there are "OutOfMemory" error.
The principle and implementation of Java thread pool
Simple Introduction
There are two ways to create a thread: Inherit thread or implement Runnable. Thread implements the Runnable interface and provides an empty run () method, so either inherit thread or implement runnable, have your own run () method.
Once a thread is created, the start () method is run (execute the Run () method), the call wait enters the waiting or call sleep enters the hibernation period, runs smoothly or the hibernation is interrupted or an exception occurs during the run.
Wait and sleep comparisons:
The Sleep method is as follows: Millis (long), sleeping (long Millis, long Nanos), the current thread enters the hibernation period, pauses execution, but the thread continues to have ownership of the monitoring resource after calling the Hibernate method. The thread continues to execute until the hibernation time is reached. The thread exits if another thread is interrupted during the hibernation period.
The wait method has a waiting (), a (long timeout), a (long timeout, long Nanos), and after the call to method, the thread discards the ownership of the monitoring resource into the standby state;
Wait (): Waits for another thread to call notify () or Notifyall () into the dispatch state to compete with other threads for monitoring. Wait () is equal to wait (0) and wait (0, 0).
Wait (long timeout): The thread enters the dispatch state when another thread calls notify () or Notifyall (), or when the time arrives in timeout milliseconds, or if another thread interrupts the thread.
Wait (long timeout, long Nanos): is equal to the (1000000*timeout + Nanos), but the time unit is nanosecond.
Thread pool:
Multithreading technology mainly solves the problem of multiple threads executing in the processor unit, which can significantly reduce the idle time of the processor unit and increase the throughput capacity of the processor unit.
Suppose a server completes a task by the time it takes to create the thread time, T2 the time of the task in the thread, T3 destroy the thread time. T1
if: T1 + T3 is much larger than T2, you can use a thread pool to improve server performance.
A thread pool consists of the following four basic components:
1. Thread pool Manager (ThreadPool): for creating and managing thread pools, including creating thread pools, destroying thread pools, and adding new tasks;
2, Work thread (Poolworker): Thread pool threads, in the absence of tasks in the waiting state, you can cycle the implementation of the task;
3, Mission Interface (Task): Each task must implement the interface for work thread scheduling task implementation, it mainly stipulates the entry of the task, the task after the completion of the work, the implementation of the status of the task;
4. Task Queue (Taskqueue): For storing tasks that are not processed. Provides a buffer mechanism.
Thread pooling technology is focused on how to shorten or adjust the t1,t3 time technology, thereby improving server program performance. It arranges t1,t3 at the start and end of a server program, or some idle time, so that there is no t1,t3 overhead when a server program processes a client request.
The thread pool not only adjusts the time period generated by T1,T3, but it also significantly reduces the number of threads created, looking at an example:
Suppose a server handles 50,000 requests a day, and each request requires a separate thread to complete. In the thread pool, the number of threads is generally fixed, so the total number of threads generated will not exceed the number of thread pool threads, and the total number of threads is 50000 if the server does not use the thread pool to process these requests. The general thread pool size is far less than 50000. Therefore, server programs that utilize thread pools do not waste time processing requests in order to create 50000, thereby increasing efficiency.