Java thread pool example

Source: Internet
Author: User

The Java thread pool is available from Java 5, and the thread pool is actually the thread container. A task is added to the thread pool using the execute (Runnable) method. A task is a Runnable object. The execution method of a task is the run () method of a Runnable object.

Note: Processing Mechanism

When a task is added to the thread pool through the execute (Runnable) method:
If the number of threads in the thread pool is smaller than corePoolSize, a new thread should be created to process the added tasks even if all threads in the thread pool are idle.
If the number in the thread pool is equal to corePoolSize, but the Buffer Queue workQueue is not full, the task is put into the buffer queue.
If the number in the thread pool is greater than corePoolSize, the Buffer Queue workQueue is full, and the number in the thread pool is smaller than maximumPoolSize, a new thread is created to process the added task.
If the number in the thread pool is greater than corePoolSize, the Buffer Queue workQueue is full, and the number in the thread pool is equal to maximumPoolSize, the handler policy is used to process the task.
When many tasks are processed:
Core Thread corePoolSize, task queue workQueue, and maximumPoolSize. If all three are full, handler is used to process the rejected task.
When there are few tasks to process:
When the number of threads in the thread pool is greater than corePoolSize, if the idle time of a thread exceeds keepAliveTime, the thread will be terminated. In this way, the thread pool can dynamically adjust the number of threads in the pool.

Step 2 (CODE)

 

The Code is as follows: Copy code

Import java. util. concurrent. BlockingQueue;
Import java. util. concurrent. Define blockingqueue;
Import java. util. concurrent. ThreadPoolExecutor;
Import java. util. concurrent. TimeUnit;

/**
* Thread pool example
*
* @ Author Msquirrel
*
*/
Public class TestThreadPoolExecutor {

Public static void main (String arg []) {

// BlockingQueue <Runnable> bq = new LinkedBlockingQueue <Runnable> ();
// ThreadPoolExecutor tpe = new ThreadPoolExecutor (3, 6, 1,
// TimeUnit. DAYS, bq );
/**
* Parameter 1: indicates the size of the reserved thread pool.
* Parameter 2: indicates the maximum size of the thread pool.
* Parameter 3: the idle time allowed by the thread pool maintenance thread.
* Parameter 4: the unit of idle time allowed by the thread pool maintenance thread.
* Fifth parameter: indicates the queue for storing tasks.
* Parameter 6: The processing policy of the thread pool to reject tasks. The default value is ThreadPoolExecutor. AbortPolicy ().
*/
ThreadPoolExecutor tpe = new ThreadPoolExecutor (3, 6, 1, TimeUnit. DAYS,
New inclublockingqueue <Runnable> (), new ThreadPoolExecutor. DiscardOldestPolicy ());

/**
* Cyclically add threads
*/
For (int I = 0; I <20; I ++ ){
// Add the thread to the thread container
Tpe.exe cute (new Runnable (){

@ Override
Public void run (){
// TODO Auto-generated method stub
Try {
// Sleep for 1 second
Thread. sleep (1000 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

System. out. println (String. format ("thread % d finished ",
This. hashCode ()));
}
});
}

/**
* After the shutdown () method is called, the main thread ends immediately, and the thread pool continues to run until all tasks are executed. If you do not call shutdown ()
* Method, the thread pool will remain, so that new tasks can be added at any time. (The shutdown () method does not block)
*/
Tpe. shutdown ();
}

}

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.