Java thread pool implementation

Source: Internet
Author: User

I used to implement a simple multi-threaded mechanism. Before I started, I would like to talk about the principles. Here is what I found on IBM developers.

Technical Background of Thread Pool

In object-oriented programming, it takes a lot of time to create and destroy objects, because creating an object requires obtaining memory resources or other resources. Even more so in Java, virtual machines will try to track every object so that garbage collection can be performed after the object is destroyed. Therefore, one way to improve service program efficiency is to minimize the number of times objects are created and destroyed, especially the creation and destruction of resource-consuming objects. How to use existing objects to serve is a key problem that needs to be solved. In fact, this is the reason why some "pooled resources" technologies are generated.

Multithreading technology mainly solves the problem of multiple threads in a processor unit. It can significantly reduce the idle time of the processor unit and increase the throughput of the processor unit. However, improper multi-threaded application will increase the processing time for a single task. Here is a simple example:

Assume that the time for completing a task on a server is t

T1 thread creation time t2 thread execution time, including the time required for Inter-thread synchronization T3 thread destruction time

Obviously T = t1 + T2 + T3. Note that this is an extremely simplified assumption.

We can see that T1 and T3 are the overhead of multithreading itself. We are eager to reduce the time consumed by T1 and T3, thus reducing t time. But some threads do not notice this, So threads are frequently created or destroyed in the program, which leads to a considerable proportion of T1 and t3 in T. Obviously this highlights the thread's weakness (T1, T3), rather than the advantage (concurrency ).

The thread pool technology focuses on how to shorten or adjust T1 and T3 time to improve the performance of server programs. It arranges T1 and t3 in the start and end time periods or some idle time periods of the server program, so that when the server program processes customer requests, there will be no overhead of T1 and T3.

The thread pool not only adjusts the time periods generated by T1 and T3, but also significantly reduces the number of created threads. Let's look at an example:

Assume that a server processes 50000 requests a day, and each request requires a separate thread. We compare the total number of threads produced when the server that uses the thread pool technology and is not conducive to the thread pool technology processes these requests. In the thread pool, the number of threads is generally fixed, so the total number of threads generated will not exceed the number or upper limit of threads in the thread pool (hereinafter referred to as the thread pool size ), if the server does not use the thread pool to process these requests, the total number of threads is 50000. Generally, the thread pool size is much smaller than 50000. Therefore, the server programs using the thread pool do not waste time processing requests to create 50000, thus improving efficiency.

These are assumptions and cannot fully explain the problem. Next I will discuss the simple implementation of the thread pool and compare and test the program to illustrate the advantages of thread technology and application fields.

A simple thread pool generally contains at least the following components:

  • Threadpoolmanager: used to create and manage a thread pool.
  • Workthread: thread in the thread pool
  • Task: A required interface for each task to be executed by a worker thread.
  • Task queue: used to store unprocessed tasks. Provides a buffer mechanism.

The thread pool manager has at least the following functions: Create a thread pool, destroy a thread pool, and add new tasks. Below is the implementation of the younger brother, or welcome to make a Brick:

Public class threadpoolmanager {
Private Static threadpoolmanager instance = NULL;
Private list <upload> taskqueue = collections. synchronizedlist (New queue list <upload> (); // task queue
Private workthread [] workqueue; // working thread (the thread that actually executes the task)
Private Static int worker_num = 5; // Number of worker threads (the default number of worker threads is 5)
Private Static int worker_count = 0;

Private threadpoolmanager (){
This (5 );
}
Private threadpoolmanager (INT num ){
Worker_num = num;
Workqueue = new workthread [worker_num];
For (INT I = 0; I <worker_num; I ++ ){
Workqueue [I] = new workthread (I );
}
}

Public static synchronized threadpoolmanager getinstance (){
If (instance = NULL)
Instance = new threadpoolmanager ();
Return instance;
}

Public void addtask (upload task ){
// Lock operations on the task queue
Synchronized (taskqueue ){
If (task! = NULL ){
Taskqueue. Add (task );
Taskqueue. policyall ();
System. Out. println ("task id" + task. getinfo () + "Submit! ");
}

}
}

Public void batchaddtask (upload [] tasks ){
// Lock the modification operation on the task queue
Synchronized (taskqueue ){
For (upload E: tasks ){
If (E! = NULL ){
Taskqueue. Add (E );
Taskqueue. policyall ();
System. Out. println ("task id" + E. getinfo () + "Submit! ");
}
}
}
}

Public void destory (){
System. Out. println ("pool begins to destory ...");
For (INT I = 0; I <worker_num; I ++ ){
Workqueue [I]. stopthread ();
Workqueue [I] = NULL;
}
// Lock operations on the task queue
Synchronized (taskqueue ){
Taskqueue. Clear ();
}

System. Out. println ("pool ends to destory ...");
}

Private class workthread extends thread {
Private int taksid;
Private Boolean isruning = true;
Private Boolean iswaiting = false;



Public workthread (INT taskid ){
This. taksid = taskid;
This. Start ();
}

Public Boolean iswaiting (){
Return iswaiting;
}
// If the thread cannot be terminated immediately when the task is in progress, exit the run () method when the isruning is detected to be false after the task is completed.
Public void stopthread (){
Isruning = false;
}

@ Override
Public void run (){
While (isruning ){
Upload temp = NULL;
// Lock operations on the task queue
Synchronized (taskqueue ){
// The task queue is empty. Wait for the new task to join.
While (isruning & taskqueue. isempty ()){
Try {
Taskqueue. Wait (20 );
} Catch (interruptedexception e ){
System. Out. println ("interruptedexception occre ...");
E. printstacktrace ();
}
}
If (isruning)
Temp = taskqueue. Remove (0 );
}
// When you wait for a new task to join, terminate the thread (call the stopthread function) and cause temp = NULL.
If (temp! = NULL ){
System. Out. println ("task info:" + temp. getinfo () + "is ining ");
Iswaiting = false;
Temp. uploadpic ();
Iswaiting = true;
System. Out. println ("task info:" + temp. getinfo () + "is finished ");
}
}
}
}
}

Then define the task interface: Here I define the function interface for uploading images (here the abstract class or interface is used as your own ).

Upload

public abstract class Upload {
protected String info;
abstract boolean uploadPic();
public String getInfo(){
return info;
}
}

Then define the specific task class: I am simple here to make it sleep for 2 s. Of course, you can also define many task classes that implement upload.

Taskupload

public class TaskUpload extends Upload {

public TaskUpload(String info){
this.info = info;
}
public String getInfo(){
return info;
}
@Override
public boolean uploadPic() {
// TODO Auto-generated method stub
System.out.println(info+"sleep begin ....");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(info+"sleep end ....");
return false;
}
}

Finally, test this simple thread pool:

public class ThreadPoolManagerTest {


public static void main(String[] args) {
// TODO Auto-generated method stub
Upload[] tasks = createBatchTask(7);
ThreadPoolManager pool = ThreadPoolManager.getInstance();
pool.BatchAddTask(tasks);
pool.destory();
}
private static Upload[] createBatchTask(int n){
Upload[] tasks = new TaskUpload[n];
for(int i = 0;i<n ;i++ ){
tasks[i] = new TaskUpload("task id is "+ i);
}
return tasks;
}
}

Applicable scope and precautions of thread pool technology

Application Scope of thread pool:

  1. A large number of threads are required to complete the task, and the time to complete the task is relatively short. The thread pool technology is very suitable for Web servers to complete such tasks as web page requests. Because a single task is small and the number of tasks is huge, you can imagine the number of clicks on a popular website. However, for long-time tasks, such as a telnet connection request, the advantages of the thread pool are not obvious. Because the telnet session time is much longer than the thread creation time.
  2. Applications with demanding performance requirements, such as requiring servers to quickly respond to customer requests.
  3. Accept a large number of sudden requests, but the server will not generate a large number of thread applications. A large number of sudden client requests will generate a large number of threads without a thread pool. Although theoretically the maximum number of threads in most operating systems is not a problem, producing a large number of threads in a short time may limit the memory, and the error "outofmemory" appears.

Reference: Introduction and simple implementation of Thread Pool

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.