Thread Pool Threadpoolexecutor Finishing

Source: Internet
Author: User
Tags throw exception

The project is used to the thread pool, but in fact many people are not familiar with the principle, here just tidy up

Threadpoolexecutor

The Java.uitl.concurrent.ThreadPoolExecutor class is the core class in the thread pool.

    • Construction method
     Public Threadpoolexecutor (int  corepoolsize,                              int  maximumpoolsize,                              Long  KeepAliveTime,                              timeunit unit,                              blockingqueue<Runnable> workQueue,                              Threadfactory threadfactory) {        this(corepoolsize, maximumpoolsize, KeepAliveTime, Unit, WorkQueue,             threadfactory, DefaultHandler);    }
    • Parameters

Corepoolsize Number of core threads
Maximumpoolsize Maximum number of threads blocked queue does not fit after the number of threads included Corepoolsize
KeepAliveTime the time that the current idle thread in the thread pool expires after a task has completed service. If the time is long enough, other tasks may be served
Unit time Units
WorkQueue the number of blocked queue threads is larger than the core thread and placed in the queue
Threadfactory Thread Pool Factory
Handler deny policy blocked queue full, also reached maximum number of threads to execute a deny policy

      • Corepoolsize: The size of the core pool, after the thread pool has been created, is created by default when no task arrives, after the thread pool has been created, the number of threads in the thread pools is 0, unless Prestartallcorethreads () is called or the Prestartcorethread () method to pre-create the thread. 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;
      • KeepAliveTime: Indicates the maximum length of time a thread will be terminated without a task executing. By default, KeepAliveTime only works if the number of threads in the thread pool is greater than corepoolsize , until the number of threads in the thread pool is not greater than corepoolsize. That is, when the number of threads in the thread pool is greater than corepoolsize, if a thread is idle for KeepAliveTime, it terminates until the number of threads in the thread pool does not exceed corepoolsize. However, if the Allowcorethreadtimeout (Boolean) method is called, the KeepAliveTime parameter also works when the number of threads in the thread pool is not greater than corepoolsize, until the number of threads in the thread pool is 0;
    • Process

1) Create a new thread when the pool size is less than corepoolsize and process the request
2) When the pool size equals corepoolsize, put the request into the workqueue, the free thread in the pool goes from the Workqueue to take the task and handle
3) When the Workqueue does not fit into the new task, the new thread sinks into the pool and processes the request (without waiting for the queue), and if the pool size is propped up to maximumpoolsize, use Rejectedexecutionhandler to do the rejection.
4) In addition, when the number of threads in the pool is greater than corepoolsize, the extra thread waits for KeepAliveTime for a long time and destroys itself if no request can be processed.

Processing steps: Core thread << blocking queue << Max threads

    • Popular Process Interpretation

If there is a factory, there are 10 workers in the factory, each worker can only do one task at the same time. So as long as 10 workers are idle, the task is assigned to the idle workers;
When 10 workers have a task to do, if the task is still in the queue to wait for the task;
If the number of new tasks is growing much faster than the workers do, then the factory supervisor may want to remedy the situation, such as re-recruit 4 temporary workers to come in;
The task is then assigned to the 4 temporary workers;
If it is not enough to say that 14 workers are doing the task, the factory supervisor may want to consider no longer accepting new tasks or abandoning some of the previous tasks.
When one of the 14 workers is free, and the new task grows at a slower pace, the factory manager may consider quitting 4 temporary workers, keeping the original 10, after all, to ask for extra workers to spend money.
The corepoolsize in this example is 10, and Maximumpoolsize is 14 (10+4).

That is, Corepoolsize is the size of the thread pool, and maximumpoolsize, in my opinion, is a remedial measure of the thread pool, that is, when the task volume is suddenly too large.

    • blocking queues

1) Arrayblockingqueue: An array-based FIFO queue, which must specify a size when created;

2) Linkedblockingqueue: A list-based FIFO queue, which defaults to Integer.max_value if this queue size is not specified when it is created;

3) Synchronousqueue: This queue is special, it does not save the submitted tasks, but will create a new thread directly to perform the new task

    • reject policy four kinds

AbortPolicy defaults to discard and throw exceptions directly
Discardpolicy Direct Discard not throw exception
Callerrunspolicy execution in the main thread
Discardoldestpolicy discards the oldest in the registration queue to perform the current
A custom policy implementation can be Rejectedexecutionhandler

    • Test case
 Public classTest { Public Static voidMain (string[] args) {Threadpoolexecutor executor=NewThreadpoolexecutor (5, 10, 200, Timeunit.milliseconds,NewArrayblockingqueue<runnable> (5));  for(inti=0;i<15;i++) {MyTask MyTask=NewMyTask (i);             Executor.execute (MyTask); System.out.println ("Thread pool Threads:" +executor.getpoolsize () + ", Number of tasks waiting to be executed in queue:" +executor.getqueue (). Size ()+ ", the number of other tasks performed:" +Executor.getcompletedtaskcount ());     } executor.shutdown (); }} classMyTaskImplementsRunnable {Private intTasknum;  PublicMyTask (intnum) {         This. Tasknum =num; } @Override Public voidrun () {System.out.println ("Executing task" +tasknum); Try{Thread.CurrentThread (). Sleep (4000); } Catch(interruptedexception e) {e.printstacktrace (); } System.out.println ("Task" +tasknum+ "Execution Complete"); }}
    • Execution results
Executing task 0thread pool Number of threads:1, number of tasks waiting to be executed in queue: 0, Number of tasks performed: 0thread pool Number of threads:2, number of tasks waiting to be executed in queue: 0, Number of tasks performed: 0Executing task1thread pool Number of threads:3, number of tasks waiting to be executed in queue: 0, Number of tasks performed: 0Executing task2thread pool Number of threads:4, number of tasks waiting to be executed in queue: 0, Number of tasks performed: 0Executing task3thread pool Number of threads:5, number of tasks waiting to be executed in queue: 0, Number of tasks performed: 0Executing task4thread pool Number of threads:5, number of tasks waiting to be executed in queue: 1, number of tasks performed: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 2, number of tasks performed: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 3, Number of tasks performed: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 4, number of tasks performed: 0thread pool Number of threads:5, number of tasks waiting to be executed in queue: 5, number of tasks performed: 0thread pool Number of threads:6, number of tasks waiting to be executed in queue: 5, number of tasks performed: 0Executing task10thread pool Number of threads:7, number of tasks waiting to be executed in queue: 5, number of tasks performed: 0Executing task11thread pool Number of threads:8, number of tasks waiting to be executed in queue: 5, number of tasks performed: 0Executing task12thread pool Number of threads:9, number of tasks waiting to be executed in queue: 5, number of tasks performed: 0Executing task13thread pool Number of threads:10, number of tasks waiting to be executed in queue: 5, number of tasks performed: 0Executing task14Task 3 completes execution of task 0 completes Task 2 execution complete Task 1 Executing task8Executing task7Executing task6Executing task5Task 4 completes execution of task 10 completes Task 11 execution complete Task 13 execution completed task 12 executing task9Task 14 finishes execution of task 8 completes Task 5 execution Complete Task 7 execution complete Task 6 execution complete
  • How to properly configure the thread pool
    • To properly configure the thread pool, you must first analyze the task characteristics, which can be analyzed from the following angles:

        1. Nature of tasks: CPU-intensive tasks, IO-intensive tasks, and hybrid tasks.
        2. Priority of tasks: high, Medium, and low.
        3. Task execution time: long, medium and short.
        4. task dependencies: Whether to rely on other system resources, such as database connections.

      Tasks of a different nature can be handled separately by thread pools of different sizes. CPU-intensive tasks are configured with as few threads as possible, such as configuring thread pooling for ncpu+1 threads. IO-intensive tasks are configured as many threads as possible, such as 2*NCPU, due to the need to wait for IO operations and the thread is not always performing the task. Mixed-type tasks, if they can be split into a CPU-intensive task and an IO-intensive task, as long as the time difference between the two tasks is not too large, then the throughput rate after decomposition is higher than the serial execution throughput rate, if the two task execution time is too large, it is not necessary to decompose. We can get the number of CPUs for the current device through the Runtime.getruntime (). Availableprocessors () method.

      Tasks with different priority levels can be handled using the priority queue Priorityblockingqueue. It allows high-priority tasks to be executed first, and it is important to note that low-priority tasks may never be executed if a task with a high priority is committed to the queue.

      Tasks with different execution times can be handled by different sizes of thread pools, or priority queues can be used to perform tasks that have a short execution time.

      A task that relies on the database connection pool, because the thread submits the SQL and waits for the database to return the results, and the longer the CPU idle time waits, the greater the number of threads should be, so that the CPU can be better utilized.

      It is recommended to use bounded queue , the bounded queue can increase the stability and early warning ability of the system, can be set to a larger point, such as thousands of. One time the queue and thread pool of the background task thread pool used by our group were full, and threw out the exception of the task, and by finding out that there was a problem with the database, the execution of SQL became very slow, because the tasks of the background task line constructor all needed to query and insert data into the database. So the work thread that causes the line constructor all blocks, the task backlog is constructor online. If we were to set up the unbounded queue, the thread pool would have more and more queues, potentially filling up the memory, making the whole system unusable, not just a background task. Of course all our system tasks are deployed with separate servers, and we use thread pools of different sizes to run different types of tasks, but this problem can also affect other tasks.

  • Best practices
    //Create thread pool to specify meaningful threading name, easy error is backtrackingThreadfactory namedthreadfactory =NewThreadfactorybuilder (). Setnameformat ("Demo-pool-%d"). build (); Executorservice Singlethreadpool=NewThreadpoolexecutor (1, 1,        0L, Timeunit.milliseconds,NewLinkedblockingqueue<runnable> (1024x768), Namedthreadfactory,NewThreadpoolexecutor.abortpolicy ()); Singlethreadpool.execute (()-System.out.println (Thread.CurrentThread (). GetName ()));  Public classTimertaskthreadextendsThread { PublicTimertaskthread () {Super. SetName ("Timertaskthread");            ... }    ... } 



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.