Several common thread pools in Java

Source: Internet
Author: User

Several common thread pools in Java

Reprint: https://www.cnblogs.com/sachen/p/7401959.html

original April 14, 2016 23:29:01
    • Label:
    • Java/
    • Thread pool/
    • Executor
    • 878
Overview:

The class used to manipulate threads in the Java built-in API is thread. There are generally two ways to create a thread,

    • Inherit thread mode

    • Implement runnable mode and create thread with runnable as target

Time-consuming tasks in Android typically require a separate thread to execute, often using a pool of threads to manage the number of threads, reuse, control execution, and cancellation.

Java thread pool

Java provides four thread pools

Newcachedthreadpool:

The thread pool can be cached, if the thread pool length exceeds the processing needs, then the empty thread is reclaimed, otherwise a new thread is created, and the size of the threads can be infinitely large.

ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    • 1

When the first task is completed while performing the second task, the thread that executes the first task is reused instead of each new thread.

Newfixedthreadpool:

The thread pool, which controls the maximum number of concurrent threads, waits in the queue for the exceeded thread.

ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
    • 1

The size of a fixed-length pool is best set based on system resources. such as Runtime.getruntime (). Availableprocessors ().

Newscheduledthreadpool:

Fixed-line pool, support timing and periodic task execution, similar to timer.

ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
    • 1

Usage examples:

ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);//表示延迟1秒后每3秒执行一次。scheduledThreadPool.scheduleAtFixedRate(new Runnable() {    @Override    public void run() { System.out.println("delay 1 seconds, and excute every 3 seconds"); }}, 1, 3, TimeUnit.SECONDS);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
Newsinglethreadexecutor:

Single thread pool, support FIFO, LIFO, priority policy.

ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
    • 1

By observing the source code, four of these threads are created to create a threadpoolexecutor. Where Threadpoolexecutor is the implementation class for the Executorservice interface.

Java.util.concurrent

This package is Java's concurrent programming package, which defines three executor interfaces

Executor: A simple interface to run a new task.
Executorservice: extended the Executor interface. Added some methods to manage the life cycle of the executor and the life cycle of the task.
Scheduledexecutorservice: Extended the Executorservice. Support Future and perform tasks on a regular basis.

Implementation classes include: Scheduledthreadpoolexecutor, Threadpoolexecutor.
The four thread pools available in Java, in addition to Scheduledthreadpool, use Scheduledthreadpoolexecutor, others are threadpoolexecutor.

Threadpoolexecutor

public ThreadPoolExecutor(int corePoolSize,                              int maximumPoolSize,                              long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

Parameter description
Corepoolsize: The number of core threads for the thread pool.
Maximumpoolsize: The maximum number of threads that the thread pool can hold.
KeepAliveTime: The timeout length when non-core threads are idle. Beyond that time, non-core threads are recycled.
The time unit of the unit:keepalivetime.
WorkQueue: The task queue in the thread pool.
Threadfactory: Thread factory, default value defaultthreadfactory.
Handler: Saturation policy, when the number of thread pools is greater than maximumpoolsize, the processing policy for the rejected task, the default value Threadpoolexecutor.abortpolicy ().

Reference:
Java Concurrency Tutorial (Oracle official profile)

Trinea:java (Android) thread pool

Java Multithreading: Threadpoolexecutor detailed

Concurrent Programming Network-ifeve.com

Thread Pool Threadpoolexecutor Introduction

Several common thread pools in Java

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.