Java Foundation perplexed Java-thread pool

Source: Internet
Author: User

Simple principle

A relatively simple thread pool should contain at least the thread pooling manager, worker threads, task queues, task interfaces , and so on.

    • The role of the thread pool manager (ThreadPool Manager) is to create, destroy, and manage thread pools, putting worker threads into the thread pool;
    • A worker thread is a thread that can loop through a task and waits while there is no task;
    • The role of the task queue is to provide a buffer mechanism to place tasks that are not processed in the task queue;
    • The task interface is the interface that each task must implement, which is used to specify the entry of the task, the finishing work after the completion of the task, the execution state of the task, and so on, and the worker thread dispatches the task execution through the interface.
benefits of the thread pool
    1. Reduce resource consumption by reusing created threads to reduce the consumption of thread creation and destruction;
    2. Increase the response speed, when the task arrives, the task can not need to wait until the thread creation can be executed immediately;
    3. Improve thread manageability, threads are scarce resources, if unlimited creation, not only consumes system resources, but also reduces the stability of the system, using the thread pool can be unified allocation, tuning and monitoring.
Example

Code used in the previous project

 PublicIntegerRepayunprocessborrowrepaytaskids(BooleanIsmultithread) {intUpdateoverduebadloanthreadpoolsize = borrow_repay_thread_pool_size;//Spit A notch, the abuse of Redis, write to the configuration file is not better? String updateoverduebadloanthreadpoolsize_s = Redisservice.get (constants.borrow_repay_thread_pool_size);if(Stringutil.isnotempty (updateoverduebadloanthreadpoolsize_s))        {updateoverduebadloanthreadpoolsize = Integer.parseint (updateoverduebadloanthreadpoolsize_s); } log.info ("Borrow_repay_thread_pool_size:"+ updateoverduebadloanthreadpoolsize);//------1 thread pool initialization codeExecutorservice Executorservice = Executors.newfixedthreadpool (updateoverduebadloanthreadpoolsize);int_counter =0; list<integer> borrowrepaytaskids = Borrowrepaytaskservice.getunprocessborrowrepaytaskids ( Updateoverduebadloanthreadpoolsize);if(Stringutil.isnotempty (Borrowrepaytaskids)) {Log.info ("Repayunprocessborrowrepaytaskids ready to process, Count:"+ borrowrepaytaskids.size ()); for(Integer borrowrepaytaskid:borrowrepaytaskids) {_counter++;if(Ismultithread && updateoverduebadloanthreadpoolsize >1) {//Multithreading                //--------2 thread pool execution codeExecutorservice.execute (NewBorrowrepaytaskthread ( This, Borrowrepaytaskid)); }Else{//Single threadRepaybytask (Borrowrepaytaskid); }            }        }Else{Log.info ("Repayunprocessborrowrepaytaskids ready to process, count:0"); } executorservice.shutdown ();Try{ while(!executorservice.awaittermination (Ten, timeunit.seconds)) {Log.info ("Waiting repayunprocessborrowrepaytaskids thread pool close ...");//Waiting thread pool close ...}        }Catch(Interruptedexception ex) {//should not reach hereEx.printstacktrace ();            Executorservice.shutdownnow ();        Thread.CurrentThread (). interrupt (); }Catch(Exception ex) {//should not reach hereEx.printstacktrace ();        Executorservice.shutdownnow (); } log.info ("Repayunprocessborrowrepaytaskids Done, Count:"+ _counter);return_counter; }
    1. The executors class provides several static methods to create a thread pool;
    2. The Newfixedthreadpool thread pool is used here. The role of Newfixedthreadpool is to create a fixed-size thread pool that creates a thread each time a task is committed, until the thread reaches its maximum size. Once the size of the thread pool has reached its maximum, it will remain unchanged, and if a thread ends up performing an exception, then a new thread will be added to the pool.
    3. Executorservice is a thread pool interface
several common thread pools

Newsinglethreadexecutor
Creates a single threaded pool of threads. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted.

Newfixedthreadpool (Common)
Creates a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. Once the maximum size of the thread pool is reached, the thread pool will be replenished with a new thread if it ends up executing an exception.

Newcachedthreadpool
Creates a cacheable pool of threads. If the size of the thread pool exceeds the thread required to process the task, then a partially idle (60 second non-performing task) thread is reclaimed, and when the number of tasks increases, the thread pool can intelligently add new threads to handle the task. This thread pool does not limit the size of the thread pool, and the thread pool size is entirely dependent on the maximum thread size that the operating system (or JVM) can create.

Newscheduledthreadpool
Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks.

Newfixedthreadpool Bottom -level implementation
 Public StaticExecutorserviceNewfixedthreadpool(intNthreads) {return NewThreadpoolexecutor (Nthreads, Nthreads,0L, Timeunit.milliseconds,NewLinkedblockingqueue<runnable> ()); } Public Threadpoolexecutor(intCorepoolsize,intMaximumpoolsize,LongKeepAliveTime, Timeunit unit, blockingqueue<runnable> work Queue) { This(Corepoolsize, Maximumpoolsize, KeepAliveTime, Unit, WorkQueue, Executors.defaultthreadfactory (), Defaulthandl    ER); } Thisis called by the Public Threadpoolexecutor(intCorepoolsize,intMaximumpoolsize,LongKeepAliveTime, Timeunit unit, blockingqueue<runnable> work Queue, Threadfactory threadfactory, Rejectedexecutionhandler Han Dler) {if(Corepoolsize <0|| Maximumpoolsize <=0||            Maximumpoolsize < Corepoolsize | | KeepAliveTime <0)Throw NewIllegalArgumentException ();if(WorkQueue = =NULL|| Threadfactory = =NULL|| Handler = =NULL)Throw NewNullPointerException (); This. corepoolsize = corepoolsize; This. maximumpoolsize = maximumpoolsize; This. workQueue = WorkQueue; This. KeepAliveTime = Unit.tonanos (KeepAliveTime); This. threadfactory = Threadfactory; This. handler = handler; }

Parameter explanation:

    • corepoolsize -The number of threads that are saved in the pool, including idle threads. (Passed through the application parameters.)
    • maximumpoolsize -The maximum number of threads allowed in the pool. (Consistent with the number of corepoolsize)
    • KeepAliveTime -When the number of threads is greater than the core, this is the maximum time to wait for a new task before terminating the extra idle thread. (This is 0L)
    • Unit-the time units of the KeepAliveTime parameter.
    • WorkQueue -the queue used to hold the task before execution. This queue is only Runnable tasks that are committed by the Execute method.
      (Newfixedthreadpool is using linkedblockingqueue thread-safe blocking queue)
    • Handler -the handler that is used when execution is blocked because the thread range and queue capacity are exceeded.
      (Newfixedthreadpool is using Rejectedexecutionhandler)

Say a little bit about corepoolsize:
The size of the core pool, this parameter is very much related to the implementation principle of the thread pool described later. After the thread pool has been created, by default, there are no threads in the thread pools, instead of waiting for a task to be created to perform the task, unless the prestartallcorethreads () or Prestartcorethread () method is called. As you can see from the names of these 2 methods, the meaning of the pre-created thread is to create a corepoolsize thread or a thread before the task arrives. By default, after the thread pool has been created, the number of threads in the threads pools is 0, and when a task comes, a thread is created to perform the task, and when the number of threads in the thread pool reaches corepoolsize, the incoming task is placed in the cache queue.

Linkedblockingqueue: A blocking queue based on a linked list structure, which sorts elements in FIFO (first-out), with throughput typically higher than arrayblockingqueue. The static Factory Method Executors.newfixedthreadpool () uses this queue.

the main workflow of the thread pool

Can be seen using the Threadpoolexecutor method to achieve, in-depth study to understand the specific implementation principle.

    1. First, the thread pool determines if the base thread pool is full? Not full, create a worker thread to perform the task. Full, then go to the next process.
    2. Next the thread pool determines if the work queue is full? is not full, the newly submitted task is stored in the work queue. Full, then go to the next process.
    3. The last thread pool determines if the entire thread pool is full? is not full, a new worker thread is created to perform the task, and the saturation policy is assigned to handle the task.
thread pool Execution Task method
 Public void Execute(Runnable command) {if(Command = =NULL)Throw NewNullPointerException ();//If the number of threads is less than the number of basic threads, create the thread and perform the current task    if(poolsize >= corepoolsize | |!addifundercorepoolsize (command)) {if(Runstate = = RUNNING && workqueue.offer (command)) {if(runstate! = RUNNING | | poolsize = =0) ensurequeuedtaskhandled (command); }//If the thread pool is not running or the task cannot be placed in the queue, and the current number of threads is less than the maximum allowable number of threads, a thread is created to perform the task.         Else if(!addifundermaximumpoolsize (command)) reject (command);//Is shutdown or saturated}}
Worker Threads

When the thread pool creates threads, the thread is encapsulated as a worker thread worker,worker the tasks in the work queue are executed indefinitely after the task is completed. We can see this from the worker's Run method.

publicvoidrun() {        try {            Runnable task = firstTask;            null;            whilenullnull) {                runTask(task);                null;            }        finally {            workerDone(this);   //当任务队列中没有任务时,进行清理工作               }    }}
thread pool configuration and monitoring

Here are some ways to find out, I think the best way to do this is through stress testing, to observe the core functions of the processing power, server CPU, IO and JVM specifics , debugging is the best.

Reasonable allocation of thread pool

To properly configure the thread pool, you must first analyze the task characteristics, which can be analyzed from the following angles:

1. 任务的性质:CPU密集型任务,IO密集型任务和混合型任务。2. 任务的优先级:高,中和低。3. 任务的执行时间:长,中和短。4. 任务的依赖性:是否依赖其他系统资源,如数据库连接。5. 任务性质不同的任务可以用不同规模的线程池分开处理。CPU密集型任务配置尽可能少的线程数量,如配置Ncpu+1个线程的线程池。IO密集型任务则由于需要等待IO操作,线程并不是一直在执行任务,则配置尽可能多的线程,如2*Ncpu。6. 建议使用有界队列。

Monitoring of the thread pool

Monitored by the parameters provided by the thread pool. Line constructor Some properties can be used when monitoring the thread pool

* taskCount:线程池需要执行的任务数量。* completedTaskCount:线程池在运行过程中已完成的任务数量。小于或等于taskCount。* largestPoolSize:线程池曾经创建过的最大线程数量。通过这个数据可以知道线程池是否满过。如等于线程池的最大大小,则表示线程池曾经满了。* getPoolSize:线程池的线程数量。如果线程池不销毁的话,池里的线程不会自动销毁,所以这个大小只增不减。* getActiveCount:获取活动的线程数。

Monitor by extending the thread pool. By inheriting the thread pool and overriding the Beforeexecute,afterexecute and terminated methods of the thread pool, we can do something before the task executes and before the thread pool shuts down. such as the average execution time of the monitoring task, the maximum execution time and the minimum execution time.

Reference :
Http://www.cnblogs.com/dolphin0520/p/3932921.html
http://ifeve.com/java-threadpool/
Http://my.oschina.net/jielucky/blog/157250?fromerr=imwl7YeR This explanation in more detail.

Java Foundation perplexed Java-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.