Java Thread Pool Learning

Source: Internet
Author: User

Reference: Creating a Java thread pool [1], Java threading: New features-thread pool [2], Java thread pool learning [3], thread pool threadpoolexecutor usage profile [4], "Example of thread pool in Java5" [5] , "Threadpoolexecutor Use and thinking" [6]

[1] The blogger himself implemented a thread pool through threadgroup (quite handy to understand), using the jdk1.4 version, the Jdk1.5 version above provides a ready-made threading pools.

The API for the Java.util.concurrent.Executors class is described in [2].

[3] Describes the Java thread pool's class architecture.

[4] There are examples of threadpoolexecutor applications.

[5] In the example of a thread pool, a port listener server based on thread pool is implemented. ★

[6] There is a detailed analysis of the source code, and good layout (^_^)

The top interface of the thread pool in Java is executor, but strictly speaking, executor is not a thread pool, but a tool for executing threads. The real thread pool interface is executorservice. The following diagram provides a complete picture of the class architecture of the thread pool.

Mark the more important classes:

Executorservice: A true thread pool interface.
Scheduledexecutorservice can be similar to Timer/timertask to solve problems that require repetitive execution of tasks.
Threadpoolexecutor The default implementation of Executorservice.
Scheduledthreadpoolexecutor Inheriting Threadpoolexecutor's Scheduledexecutorservice interface implementation, the class implementation of periodic task scheduling.

To configure a thread pool is more complex, especially if the thread pool principle is not very clear, it is likely that the thread pool configured is not superior, so there are some static factories in the executors class that generate some common thread pools.

    • Newsinglethreadexecutor: Creates a single thread pool. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted.
    • Newfixedthreadpool: Creates a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. Once the maximum size of the thread pool is reached, the thread pool will be replenished with a new thread if it ends up executing an exception.
    • Newcachedthreadpool: Creates a cacheable thread pool. If the size of the thread pool exceeds the thread required to process the task, then a partially idle (60 second non-performing task) thread is reclaimed, and when the number of tasks increases, the thread pool can intelligently add new threads to handle the task. This thread pool does not limit the size of the thread pool, and the thread pool size is entirely dependent on the maximum thread size that the operating system (or JVM) can create.
    • newscheduledthreadpool: Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks.
    • Newsinglethreadexecutor: Creates a single thread pool. This thread pool supports the need to schedule and periodically perform tasks.

That I personally feel is new Threadpoolexecutor (Core_pool_size, Max_pool_size, Keepalive_time, Time_unit, WorkQueue, Rejectedexecutionhandler) provides a more customized method of thread pool manufacturing. Because the Newfixedthreadpool method is actually return new Threadpoolexecutor

The API for the Java.util.concurrent.Executors class provides a number of static methods for creating connection pools:

1. Fixed-size thread pool:

Package BackStage;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ExecutorService;
    void Main (string[] args) {
Executorservice pool = Executors.newfixedthreadpool (2);
New MyThread ();
New MyThread ();
New MyThread ();
New MyThread ();
New MyThread ();
Pool.execute (t1);
Pool.execute (T2);
Pool.execute (T3);
Pool.execute (T4);
Pool.execute (T5);
Pool.shutdown ();
}
    @Override
System.out.println (Thread.CurrentThread (). GetName () + " executing ... ");
}

2. Single Task thread pool:

For both of these connection pools, the size is fixed, and when the thread (or task) of the pool to be joined exceeds the maximum size of the pool, the pool of incoming threads needs to be queued. Once the threads in the pool are complete, a thread queued for execution is pooled.

[2] also introduced: 3. Variable size thread pool newchahedthreadpool;4. delay connection pool; 5. Single-Task deferred connection pool; 6. Custom Thread pool

Java Thread Pool Learning

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.