Technical Research and Design Based on efficient thread pool [reposted from the Internet]

Source: Internet
Author: User

Zhu Xiang

(School of Information Science and Engineering, Central South University, Changsha 10075, China)

Abstract: Based on the extension design mode of the Object pool, an efficient thread pool is designed. The deadlock problem in the thread pool is studied.

Problems, thread allocation issues, thread leaks, and the corresponding solutions are proposed to achieve the policy management and

The efficient reuse of threads improves the system efficiency.

Key words: design mode; Object pool; thread deadlock; thread allocation; thread Leakage

Chinese map: tp311. 11 document identification code:

Research and Design Based on the highly efficient Thread Pool

Zhu Xiang
(College of Information Science and Engineering, Central South University, Changsha 4 10075, China)

Abstract: On the basis of Object pool expansion's designed pattern, the highly efficient Thread Pool

Is designed; studies are made on the thread deadlock, thread distribution, thread leak; and

Relevant resolutions are also proposed, which put s the Strategy Management of thread pool and

Reuse of thread into practice, thus enhances the efficiency of the whole system.

Keywords: design pattern; Object pool; thread deadlock; thread distribution; thread leak
In large-scale software design, some valuable resources, such as memory, threads, and database connections, are used.

The efficiency of software usage is greatly affected in time or space, so that the availability of resources and the performance and usage of client applications

Therefore, resource management is required to develop scalable, high-concurrency C/S-structured applications.

In order to make full use of these valuable resources

In application, the guiding principle of resource management is to allocate resources as late as possible and release resource allocation as early as possible.

Software designers share these resources and try to reuse them in time or space.

In large concurrent applications, a thread is a time-consuming resource, because each thread requires two

Execute the call stack. Although the scheduling overhead of switching between threads is very small, if there are many threads, the Environment switching may be serious.

Therefore, frequent thread establishment and shutdown greatly reduce the system performance.

Pool, which enables the thread to reuse in multiple requests, reduces the system running time and improves the system efficiency.

Based on the object Pool Mode of grand, the efficient thread pool is designed through the extension mode of the Object pool.

Receipt date: 2003-04-04
Author profile: Zhu Xiang 1976-, male, Hunan Shaoyang, Hunan Mobile Communication Company engineer.

The deadlock problem, thread allocation problem and thread leakage problem in, and the corresponding solution is proposed.

1. Related Principles

1. 1 object Pool Mode
Mark grand describes the design pattern of the Object pool, pointing out that the object pool is a design pattern, which uses
Similar to reusing to share some scarce or expensive resources [1]. The UML structure of this mode is divided into three parts: 1

As shown in.
Reusable: Reusable object or resource.
Client: the user of the reusable object. One client object can use multiple reusable objects, and one object can only be
Client.

Reusablepool: the object pool, that is, the container or set of reusable objects, used to manage and store reusable objects. Generally,

To ensure the security of Object reuse, the object pool is designed as a globally unique instance in singleton mode.

Figure 1 Object pool UML chart

After the object pool mode is proposed, this mode is widely used by software designers, and the connection pool and thread pool appear.

This greatly improves system performance.

1. 2 object pool extension mode
During the development process, it is found that some common attributes exist in the object Pool Mode in actual applications, such as the extension of reusable objects.

Object pool management, reuse object lifecycle management, and so on. Therefore, based on the object pool mode
These commonalities propose the extension mode of the Object pool [2, 3]. See figure 2.

In this extension mode, six new interfaces and two abstract extension objects are defined. Among them, the realobject creater interface is negative.

Responsibility extension reuse object generation and elimination, programmers can implement realobject creater abstract object

And poolmanager interfaces abstract object management policies.

Poolmanager abstracts objects and implements different management policies for different objects to create, destroy, and manage reusable objects.

On the basis of the extended mode, the author studies the generation and management policies of thread pools and extends

The realobject creater interface and the poolmanager interface enable policy management in the thread pool and efficient reuse of threads.

2. Thread creation and Elimination Design

Realobject is an abstract object used to expand new application domains in the object pool extension mode. Its main function is to complete

Creation and extinction of the image. The generation and extinction of threads have their own special characteristics, including:
1) A thread is a special object with an initial, intermediate, or extinct state.

When a thread is recycled, it must return to its initial state, rather than retaining its intermediate state.

Figure 2 object pool extension mode
2) threads may manage multiple types of resources. resources managed by all threads must be released when the threads die.

When an application needs to create a thread to complete a task, the thread pool will create a new initial thread.
The thread will not be destroyed immediately when the thread ends, but is marked as paused in the thread pool. If the application
When a program sends another thread request to the thread pool, the paused thread is awakened to complete related tasks
Create a new thread to save a lot of resources. As long as the application initiates a request to the thread pool at a speed not higher

Thread Pool does not need to create a new thread for the application and has been reusing the same thread.
In addition, if the application's workload drops or disappears, some threads in the thread pool will remain suspended.
Thread, the thread pool must correctly destroy threads that are no longer in use. To solve this problem, the algorithm is: when the thread is in the paused state
After a specified time, if no related task is assigned to the thread, the thread will be awakened, and then die for itself.
Put all resources.

In short, with the thread pool technology, if the application needs to process more tasks, the thread pool will create enough threads.
If the application load drops, the thread pool can automatically destroy these threads so that the resource usage of the operating system remains at one
Reasonable level.
Different threads have different initialization statuses and different resource release methods.
Functions are abstracted as an interface so that the thread pool can manage the initialization and resource release of thread objects. The interface is as follows:
Description:

Interface threadobject {

Init (); // thread Initialization
Releaseallresource (); // release all resources
Destroy (); // The thread disappears

}

3. Thread Pool Management Policy

Thread management is an important part of the thread pool. Different management objects need different management policies.
The particularity of use, in the extended poolmanager, must consider the special characteristics of the thread, such as the thread deadlock problem, thread Resources
Source allocation problems.
3. 1. Thread deadlock and solution policies
A deadlock occurs when every thread in A group is waiting for an event that can only be caused by another process in the group,
This group of threads cannot run normally, which is called a deadlock. For example, in a Java program, thread a holds
The exclusive lock is waiting for the lock of object y, while thread B holds the exclusive lock of object y, but waits for the lock of object X [4].

Unless there is a way to break the lock wait (J Ava does not support this method), the deadlock thread will always wait
There is another possibility of deadlock when using the thread pool, that is, when all the pool threads are in the execution of the blocked waiting queue
The execution result of a thread, but the task cannot be run because of the threads that are not occupied.
Any multi-threaded application may cause deadlocks. A mechanism must be found to break the deadlock.

Resources held by a thread in this chain to solve the resource desires of other threads.

(This thread has the highest priority). During the specified time interval, view the "Start Time
To calculate the thread usage time. If the thread usage time exceeds the set rating, the thread will be released.
All resources, and make it back to the starting state, so that other threads get the runtime time. The pseudo code of this policy is as follows:
Below:

Checkdeadlock {
Lock threadpool (); // lock the thread pool. New threads cannot be allocated.

For (for each object in the used Object pool ){
If (object. uestime> maxuestime) // If the thread usage time exceeds the specified rating

{

(Threadobject) object). releasallresource (); // release all resources mastered by this thread
(Threadobject) object). INIT (); // return to the starting state

Usepool. Release (object );
Nuusepool. Add (object );
}

}

}

3. 2. Thread Resource Allocation Policy
Compared with other alternative scheduling mechanisms, the thread pool usually runs well, but thread consumption includes memory and other systems.
A large number of resources, including resources, because each thread requires two potentially large execution call stacks; although switching between threads

The scheduling overhead is very small, but if there is a lot of multithreading, Environment switching may also seriously affect program performance. Therefore, two policies are adopted.
To adjust the thread pool size:
1) set the maximum number of threads used by the thread pool. When the number of threads in the thread pool reaches this rating

There is a request queue waiting for a new idle thread to appear in the thread pool.
2) defines the maximum idle time of a thread. If the idle time of a thread exceeds the rated value, the system

Threads do not need much. The system should release some threads to recycle resources, so that other parts of the system can obtain more resources.
For source use, it is necessary to ensure that the number of threads is above the minimum rating, otherwise the significance of the thread pool is lost.
The program's pseudo code is as follows:

Checkunusepool {
For (every idle thread ){
If (idle time> maximum idle time) and (idle quantity> Minimum number of threads ){
(Threadobject) object). Release ();
(Threadobject) object). Destroy ();
}
}
}
3. Thread Leakage
The thread pool is prone to thread leakage. The so-called thread leakage is the cause of the thread that should have been managed by the thread pool.
For some reason, it cannot be accessed by the thread pool. When this happens many times, the thread pool will eventually be empty, and

The system will stop because there is no available thread to handle the task. The main reasons for thread leakage include:
1) when the thread throws a runtimeexception or error, the thread pool does not respond to this error, or
If the user does not handle this error, the thread will exit and the number of threads in the thread pool will be greater than the actual number [4 ].

This may cause the thread pool to allocate a thread that actually does not exist, leading to system errors. Therefore, you need to check the time thread.
Whether all threads are available. If a thread does not actually exist, and the thread pool retains its "Reference" or

Pointer, you must release these "references" or pointers.
2) The thread may always wait for some resources or input from the user, and these resources cannot be guaranteed to be available, such

Such tasks will be permanently stopped, and these stopped tasks will cause the same problems as thread leaks.
The same problems are encountered in the Lock Resolution Policy, and can be solved using the same policy.

4 Conclusion

The thread pool enables efficient thread reuse and is widely used in the design and development of large-scale concurrent programs. in practical applications,
Uses time threads, controls the thread pool size, and queries the thread status to solve the deadlock and thread allocation problems in the thread pool.
And thread leakage issues, and achieved good results.

References:

[1] Mark grand. Patterns in Java [M]. Wiley: John & Sons, 1999.

[2] Shui Chao, Li Hui. Extension and Research of Object pool model [J]. Computer Engineering, 2003, (1): 76-79.

[3] Erich Gamma, Richard Helm, Ralph Johnson, et al. Design Patterns element S of reusable object-oriented software [m]

. Beijing: Machinery Industry Press, 2002.

[4] Brian Goetz. The thread pool helps achieve optimal resource utilization [Eb/Ol]. http: // www-900. IBM. com/developerworks/CN/Java/J-

Jtp0730/index. shtml, 2002-10-15.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.