A small example of self-built Thread Pool

Source: Internet
Author: User

This example is found on the network, which roughly introduces the concept of the thread pool and provides a good introduction.

However, there are some program problems, maybe the original author did not pay too much attention. I added a sentence to make the original program more reasonable,

If the original author sees it, please forgive me.

 

I 'd like to review the principle:

Threads share common variables in the process space to facilitate inter-thread communication. Different threads encapsulate different

Code, multi-threaded synchronous operation, can be seen as a synchronous operation of multiple code segments.

So what is a thread pool, the same principle as the database connection pool, is to be able to reuse existing things and reduce the system

. Therefore, in the vernacular, a container must contain these threads and an object must be used for control.

These threads.

The following program consists of three classes: A simple Thread class (responsible for specific operations) and a management Thread class (responsible for scheduling)

A client simulation class that is responsible for calling.

The Code is as follows:

A simple Thread class

Package simplethreadpool;

Public class simplethread extends thread {
 
Private Boolean runningflag;
Private string argument;
 
Public Boolean isrunning (){
Return runningflag;
}
 
Public synchronized void setrunning (Boolean flag ){

Runningflag = flag;
If (FLAG ){
This. Policy ();
}
}
 
Public String getargument (){

Return argument;
}
 
Public void setargument (string ){

Argument = string;
}
 
Public simplethread (INT threadnumber ){
Runningflag = false;
System. Out. println ("Thread" + threadnumber + "started ;");
}

Public synchronized void run (){

Try {

While (true ){

If (! Runningflag ){
This. Wait ();

} Else {

System. Out. println ("processing" + getargument () + "... done ");
Sleep (5000 );
System. Out. println ("thread is sleeping ");
Setrunning (false );
}

}

} Catch (interruptedexception e ){
// Todo: handle exception
System. Out. println ("interruptedexception ");
}

}
}

One Management Thread class

Package simplethreadpool;

Import java. util. vector;

Public class threadpoolmanager {

Private int maxthread;

Public vector;

Public void setmaxthread (INT threadcount ){
Maxthread = threadcount;

}

Public threadpoolmanager (INT threadcount ){
Setmaxthread (threadcount );
System. Out. println ("Starting thread pool ....");

Vector = new vector ();
For (INT I = 0; I <10; I ++ ){
Simplethread thread = new simplethread (I );
Vector. addelement (thread );
Thread. Start ();
}
}

Public void process (string argument ){

Int I;
For (I = 0; I <vector. Size (); I ++ ){
Simplethread currentthread = (simplethread) vector. elementat (I );
If (! Currentthread. isrunning ()){
System. Out. println ("Thread" + (I + 1) + "is processing ::"
+ Argument );
Currentthread. setargument (argument );
Currentthread. setrunning (true );
Break; // new code
}
If (I = (vector. Size ()-1 )){

System. Out. println ("pool is full, try in another time .");
}

}
}
}

One client simulation class

Package simplethreadpool;
Import java. Io .*;

Public class testthreadpool {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub

Try {

Bufferedreader BR = new bufferedreader (New inputstreamreader (system. In ));
String S;
Threadpoolmanager manager = new threadpoolmanager (10 );
While (S = Br. Readline ())! = NULL ){
Manager. Process (s );
}


} Catch (ioexception e ){
// Todo: handle exception

}

}

}

I ran it for a moment. It was really good. At least I understood it in the Framework.

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.