Underlying Implementation of the executors factory class to create a thread pool

Source: Internet
Author: User

The previous article introduced the basic usage of threadpoolexecutor. Now let's take a look at the basic usage and underlying implementation of the executors factory class.

Source code for three factory methods for creating a thread pool:

// Public static executorservice newcachedthreadpool () {return New threadpoolexecutor (0, integer. max_value, 60l, timeunit. Seconds, new synchronousqueue <runnable> ());}

A synchronousqueue with a direct submission policy is used when the number of core threads is 0, the maximum number of threads is unbounded, and keepalivetime60 seconds.
Because synchronousqueue is used, the task is continuously accepted. Because corepoolsize is set to 0,

There is no thread that can be used to run the task immediately. Therefore, a new thread is created to execute the task. Because the KeepAliveTime is set to 60 seconds.

Therefore, when a thread is reclaimed 60 seconds after it completes Task A, it may also serve other tasks. Achieve the effect of thread reuse.

// Fixed-size thread pool public static executorservice newfixedthreadpool (INT nthreads) {return New threadpoolexecutor (nthreads, nthreads, 0l, timeunit. milliseconds, new linkedblockingqueue <runnable> ());}

The number of core threads is the same as the maximum number of threads. KeepAliveTime is 0 milliseconds and the unbounded blockingqueue is used.
In such a thread pool, there are only a fixed number of core threads in the pool, and all new tasks are waiting in the queue.

// Public static executorservice newsinglethreadexecutor () {return New Executor (New threadpoolexecutor (1, 1, 0l, timeunit. milliseconds, new queue blockingqueue <runnable> ()));}

The number of core threads and the maximum number of threads are 1, and The KeepAliveTime is 0 milliseconds. the unbounded blockingqueue is used.
Similar to the above, but this thread pool has only one thread to serve it, and all new tasks are waiting in the queue.

The executors factory class provides us with an easier way to create a thread pool to meet some basic needs and to create a thread pool through threadpoolexecutor when we cannot meet business needs.

The executors class is finished. By the way, scheduledexecutorservice is used.

Such commands can be scheduled to run or periodically run after a given delay. After an example is demonstrated, the hello thread is output every three seconds. The task is completed after 10 seconds, and scheduler is disabled.

Public static void main (string [] ARGs) {final scheduledexecutorservice myschedors = executors. newscheduledthreadpool (1); // after one second, the final scheduledfuture is executed every 3 seconds. <?> Handler = myscheduler. scheduleatfixedrate (New runnable () {@ overridepublic void run () {system. out. println ("Hello thread. ") ;}}, 1, 3, timeunit. seconds); // end the task after 10 seconds and disable schedulermyscheduler. schedule (New runnable () {@ overridepublic void run () {handler. cancel (true); myscheduler. shutdown () ;}, 10, timeunit. seconds );}

For Original Articles, please indicate the source:
Http://blog.csdn.net/thinking_in_android

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.