The relationship between the core number of threads in the thread pool ThreadPoolExecutor proof, the maximum number of threads, the queue length

Several parameters of the thread pool, a lot of people are not very clear on how to configure, what is the relationship between them, I use the code to prove it.

Package Penalty for www.itbac.com; 

Import java.util.concurrent *. ; 

public  class ExecutorTest { 
    
    public  static  void main (String [] args) {
         // create a thread pool, Definition :( core number of threads, maximum number of threads, plus open thread survival time, time units, the task queue length) 
        the ThreadPoolExecutor the pool = new new the ThreadPoolExecutor (. 5,. 8 ,
                 0L , TimeUnit.MILLISECONDS,
                 new new a LinkedBlockingQueue <the Runnable> (2 )); 
        
        // set: number of tasks = 3 to 11, analysis: the relationship between the number of jobs and the number of active threads, the core number of threads, queue length, the maximum number of threads. 

        int A =. 3 ; 
    
            for ( int I =. 1; I <= A; I ++) {
                 Int J = I; 
                pool.submit ( new new the Runnable () { 
                    @Override 
                    public  void RUN () {
                         // Get the name of the thread 
                        the Thread Thread = Thread.currentThread (); 
                        String name = Thread.getName ();
                         // Output 
                        int activeCount = pool.getActiveCount (); 
                        System.out.println ( "task:" + j + "-----, thread name:" + name + "----- number of active threads:" + activeCount); 
                    }  
                });
            } 
            
        // Close thread pool 
        pool.shutdown (); 

    } 
}

 

Output, observe the relationship:

// number of tasks a = 3, number 3, number of tasks <core number of threads active threads.
// number of tasks a = 4, number 4, number of tasks <core number of threads active threads.
// number of tasks a = 5, the number of active threads 5, tasks = core number of threads.
// number of tasks a = 6, the number of active threads 5, task number <Number 5 core thread 2 + queue length.
// number of tasks a = 7, the number of active threads 5, the number of tasks = 5 core threads 2 + queue length.

// number of tasks a = 8, the number of active threads 6, the number of tasks <number of threads 8 + 2 maximum queue length. The number of active threads is based on the core number of threads 5, plus an active threads.
// number of tasks a = 9, the number of active threads 7, the number of tasks <the maximum number of threads 8 2 + queue length. The number of active threads is based on the core number of threads 5, plus two active threads.
// number of tasks a = 10, the number of active threads 8, the task number = the maximum number of threads 8 2 + queue length. The number of active threads is based on the core number of threads 5, plus three active threads.

// number of tasks a = 11, the number of active threads 8, the task number> The maximum number of threads 8 2 + queue length. Thrown RejectedExecutionException

 

to sum up:

 

With increasing number of tasks, it will increase the number of threads active.

When the number of threads active = core number of threads, now it will not increase the number of active threads, but to the task queue accumulation.

When the task queue is filled, with the increase in the number of mandates, it will provide an additional thread on the basis of the number of kernel threads.

Until the number of active threads = maximum number of threads, the thread can not be increased.

If the task is still increasing at this time, then: task number 11> the maximum number of threads 8 2 + queue length, thrown RejectedExecutionException, reject the task.

 

Guess you like

Origin www.cnblogs.com/itbac/p/11306465.html