Java Thread Pool Tool class

Source: Internet
Author: User

Benefits of using the thread pool:

    1. reusing threads, creating and destroying threads is time-consuming.

    2. Controls the number of threads.

Thread Pool Tool Class:

Threadpool.java

Package Com.zws.thread.pool;import java.util.concurrent.callable;import java.util.concurrent.future;/** * * @author Wensh.zhu * */public interface ThreadPool {void execute (Runnable Task);<t> future<t> Submit (callable<t > Task);/** * thread pool is busy * @return */boolean isBusy ();}

Simplethreadpool.java

package com.zws.thread.pool;import java.util.concurrent.callable;import  java.util.concurrent.future;import java.util.concurrent.linkedblockingqueue;import  java.util.concurrent.threadpoolexecutor;import java.util.concurrent.timeunit;/** *  *   @author  wensh.zhu * */public class SimpleThreadPool implements  THREADPOOL{PRIVATE&NBSP;STATIC&NBSP;INT&NBSP;COREPOOLSIZE&NBSP;=&NBSP;30,&NBSP;MAXIMUMPOOLSIZE&NBSP;=&NBSP;50,  capacity = 50000;private static long keepAliveTime = 30 *  1000l;private static linkedblockingqueue<runnable> workqueue;private static  threadpoolexecutor executor;private static simplethreadpool pool = new  Simplethreadpool ();p rivate simplethreadpool ()  {workqueue = new linkedblockingqueue <Runnable> (capacity); Executor = new threadpoolexecuTor (corepoolsize, maximumpoolsize, keepalivetime, timeunit.milliseconds, workqueue,  New rejectionhandler ());} Public static simplethreadpool newinstance ()  {return pool;} Public void execute (Runnable task)  {executor.execute (Task);} Public <t> future<t> submit (Callable<t> task)  {return  Executor.submit (Task);} Public boolean isqueuefull ()  {return workqueue.size ()  == capacity;} Public int queuesize ()  {return workqueue.size ();} Public boolean isbusy ()  {return executor.getpoolsize ()  == maximumPoolSize  && isqueuefull ();} Public int poolsize ()  {return executor.getpoolsize ();}}

Rejectionhandler.java

Package Com.zws.thread.pool;import Java.util.concurrent.rejectedexecutionhandler;import java.util.concurrent.threadpoolexecutor;/** * * @author Wensh.zhu * */public class Rejectionhandler implements RejectedE Xecutionhandler {@Overridepublic void rejectedexecution (Runnable R, Threadpoolexecutor executor) {System.out.println ( "Task" + R + "execute Fail");}}

Explains the parameters of the Threadpoolexecutor class constructor:

corepoolsize: The number of core threads, the number of threads that the thread pool retains, and if the allowCoreThreadTimeOut parameters are not set, the threads are not recycled even if they are idle. That is allowCoreThreadTimeOut , if the parameter is not set then the thread pool retains at least corepoolsize number of threads when the number of threads Cheng is greater than or equal to corepoolsize.

workQueue: thread pooling Work queue, when the number of thread pool threads reaches corepoolsize and those threads are busy, then the tasks submitted to the thread pool are cached to the work queue. When there are idle threads in the thread pool, the task is taken from this work queue and executed.

maximumpoolsize: The maximum number of threads allowed by the thread pool, when the work queue is full, the thread pool continues to create new threads, but the number of threads in the thread pools does not exceed maximumpoolsize.

KeepAliveTime: When the number of threads in the thread pool exceeds corepoolsize, the idle wait time for some threads is exceeded, and when the idle time exceeds this value the partial thread is reclaimed.

Unit :units of KeepAliveTime.

handler: the thread pool task rejects the policy interface, and when the work queue is full and the number of busy threads in the thread pool reaches maximumpoolsize, subsequent tasks submitted to the thread pool are rejected, and this interface is invoked to implement a specific deny policy. If this interface is not specified, the java.util.concurrent.RejectedExecutionException exception is reported.

This article is from the "Evan" blog, be sure to keep this source http://wenshengzhu.blog.51cto.com/5151285/1952144

Java Thread Pool Tool class

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.