The relationship between queue and pool size in the Java thread pool

Source: Internet
Author: User

The relationship between queue and pool size in the Java thread pool

Java threads write their own understanding of the relationship between queue, pool size, and core threads in the thread pool (threadpoolexecutor):

1: Core thread: Simply the number of threads in the thread pool that can allow concurrent runs concurrently

2: Thread pool Size: The maximum number of threads that can be accommodated in a thread pool.

3: Queue: The processing mode for the submitted task.

There is a principle for the thread pool to interact with the queue:

If the queue comes up with a task that discovers that the number of threads running in the thread pool is less than the core thread, create a new thread immediately without entering the queue waiting. If the running thread is equal to or greater than the core thread, you must refer to whether the submitted task can join the queue.

1: Can the submitted task join the queue

1) If the submitted task can join the queue, consider whether the value of the queue is set, if not set, then you can not create a new thread, can only wait in the queue, because the theoretical queue can accommodate an infinite number of tasks waiting. In other words, the number of core threads in the thread pool at this point is the maximum number of threads that can be allowed in the pool. Then the maximum number of threads in the pool doesn't make any sense.

2) If the submitted task can join the queue, the queue value is qualified, then the task enters the queue to wait, once the queue is full, the newly added task enters the thread pool to create a new thread. Once the maximum number of threads in the thread pool exceeds, the subsequent tasks are rejected.

2: If the submitted task cannot join the queue

1:) The submitted task cannot join the queue, and a new thread is created to join the thread pool, and once the maximum number of threads in the thread pool is exceeded, the task is rejected.

4: Three strategies of the queue:

        1. SynchronousQueue  直接提交,也就是上面讲到的所有任务不进入队列去等待。此时小于核心线程就增加,多于或等于核心线程数时,还是增加线程,最大为线程池中的最大允许。超出就拒绝。

        2. LinkedBlockingQueue  无界队列 此时超过核心线程后的任务全部加入队列等待,系统最多只能运行核心线程数量的线程。这种方法相当于控制了并发的线程数量。

        3. ArrayBlockingQueue   有界队列  此时超过核心线程后的任务先加入队列等待,超出队列范围后的任务就生成线程,但创建的线程最多不超过线程池的最大允许值。

5: The following source code:

A fixed number of thread pools

public static Executorservice newfixedthreadpool (int nthreads) {

return new Threadpoolexecutor (Nthreads, Nthreads,

0L, Timeunit.milliseconds,

New linkedblockingqueue<runnable> ());

}

As you can see, the core thread of this thread pool is a value to the maximum thread, not waiting for the thread to be recycled after a certain amount of time outside the core thread. A maximum number of threads running at the same time nthreads.

Single thread pool (in fact, it can be understood as a fixed thread pool with a core thread of 1)

public static Executorservice Newsinglethreadexecutor () {

return new Finalizabledelegatedexecutorservice

(New Threadpoolexecutor (1, 1,

0L, Timeunit.milliseconds,

New Linkedblockingqueue<runnable> ()));

}


Dynamic thread pool (no boundary pool)

public static Executorservice Newcachedthreadpool () {

return new Threadpoolexecutor (0, Integer.max_value,

60L, Timeunit.seconds,

New synchronousqueue<runnable> ());

}

As you can see, the core thread pool is 0, the maximum of the thread pool is infinite, the wait time is 60 seconds, and the queue is submitted directly.

That is, each time a task directly generates a thread, there is no limit to the thread, but once the thread pool has more than 60 seconds of idle threads are recycled.

In general, we generally use static methods of the Executors class to create thread pools, unless we understand the thread pool very well to be able to flexibly plan thread pool classes (which can be used to inherit Threadpoolexecutor)


This article is from the Java Learning Log blog, so be sure to keep this source http://20150523.blog.51cto.com/10261028/1654425

The relationship between queue and pool size in the 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.