Java multithreading: main factory methods for class executors

Source: Internet
Author: User

Executors class provides a series of factory methods for creating a thread pool, and the returned thread pool implements the executorservice interface.
1. newfixedthreadpool
Public static executorservice newfixedthreadpool (INT nthreads );
Public static executorservice newfixedthreadpool (INT nthreads, threadfactory)
Create a thread pool that can reuse a set of fixed threads and run these threads in a shared, unbounded queue mode. If any thread is terminated due to a failure during the execution before it is disabled, a new thread will execute subsequent tasks (if needed) in place of it ).
2. newsinglethreadexecutor
Public static executorservice newsinglethreadexecutor ();
Public static executorservice newsinglethreadexecutor (threadfactory );
Create an executor that uses a single worker thread to run this thread in an unbounded queue mode. (Note: If this single thread is terminated because of a failure during the execution before it is disabled, a new thread will execute subsequent tasks in place of it if needed ). The tasks can be executed sequentially, and no threads are active at any given time. Unlike other equivalent newfixedthreadpool (1), other threads can be used without the need to reconfigure the execution program returned by this method.
3. newcachedthreadpool
Public static executorservice newcachedthreadpool ();
Public static executorservice newcachedthreadpool (threadfactory );
Create a thread pool where new threads can be created as needed, but they will be reused when previously constructed threads are available. For programs that execute many short-term asynchronous tasks, these thread pools can usually improve program performance. Calling execute will reuse the previously constructed thread (if the thread is available ). If the existing thread is not available, create a new thread and add it to the pool. Terminate and Remove unused threads from the cache for 60 seconds. Therefore, a thread pool that remains idle for a long time does not use any resources. Note: You can use the threadpoolexecutor constructor to create a thread pool with similar attributes but different details (such as timeout parameters.

4. newscheduledthreadpool
Public static scheduledexecutorservice newscheduledthreadpool (INT corepoolsize );
Public static scheduledexecutorservice newscheduledthreadpool (INT corepoolsize, threadfactory );
Create a thread pool that can be scheduled to run commands or periodically after a given delay.
5. newsinglethreadscheduledexecutor
Public static scheduledexecutorservice newsinglethreadscheduledexecutor ();
Public static scheduledexecutorservice newsinglethreadscheduledexecutor (threadfactory );
Create a single-threaded execution program that can be scheduled to run commands or regularly after a given delay. (Note: If a single thread is terminated due to a failure during the execution before it is disabled, a new thread will execute subsequent tasks in place of it if necessary ). The tasks can be executed sequentially, and no threads are active at any given time. Unlike other equivalent newscheduledthreadpool (1), other threads can be used without the need to reconfigure the execution program returned by this method.

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.