[java parallel bag learning two] Executors Introduction The

2023-03-18  

Executors class is a very powerful tool class that starts with JDK 1.5 that is mainly used to create various thread pools.

Common method introduction

newFixedThreadPool

NEWFIXEDTHREADPOOL method has two functions signature:

public static ExecutorService newFixedThreadPool(int nThreads)  
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory)

These two functions are used to create a fixed thread pool with the largest number of threads. The thread pool uses a shared unbounded queue to store the submitted task. Parameter NTHREADS specifies the maximum number of thread pools, parameters, parametersthreadFactoryis a thread factory class, which is mainly used to customize the behavior when creating new threads in the thread pool. It should be noted that when creating a thread pool, if the thread pool does not receive any tasks, there will be no new threads in the thread pool. When the number of thread pools is less than the maximum number of threads, each new task will create a new task to create a new task. When the number of new threads reaches the maximum number of threads, it will no longer create a new thread. The new task is stored in the queue, and the number of threads will no longer change!

newWorkStealingPool

Newstealingpool method has two functions signature:

public static ExecutorService newWorkStealingPool(int parallelism)
public static ExecutorService newWorkStealingPool()

These two methods are used to create the forkjoinpool thread pool used in the forkjoin framework. The parameters in the first function are used to specify the number of parallel. The second function does not have parameters. Number of parallel.

newSingleThreadExecutor

NewsinglethreadExecutor is used to create a thread pool with only one thread. There are two function signatures:

The task in the

public static ExecutorService newSingleThreadExecutor()
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)

thread pool is stored in unbounded queue. The second function can specify ThreadFactory to customize the behavior when creating a thread.

newCachedThreadPool

NEWCACHEDTHREADPOOLF method is used to create a thread pool that can be automatically adjusted with the actual situation. There are also two types of function signatures:

public static ExecutorService newCachedThreadPool()
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)

The maximum number of threads of this thread pool is limited by the maximum number of threads that can be created by the operating system. When there are many tasks of the thread pool, they will continue to create new threads. When the task is processed, if a thread is completed, if a thread is completed, if a thread is completed, a certain thread will The free time is greater than 60s, and the thread will be destroyed. Because this thread pool can automatically adjust the number of threads, it is more suitable for performing a large number of short -term tasks.

newSingleThreadScheduledExecutor

NewsinglethReadScheDexEcutor This method is used to create a thread pool with only one thread, and the thread is scheduled to perform a given task periodic periodic. There are two types of function signatures:

public static ScheduledExecutorService newSingleThreadScheduledExecutor()
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory)

What needs to be noted is:threads if you encounter Exception when performing tasks periodically, you will no longer perform periodical tasks in the future.

newScheduledThreadPool

NewsCheDuledThreadPool is used to create a thread pool. The thread in the thread pool can periodically perform a given task. There are two function signatures:

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory)

unconfigurableExecutorService

UnconfigurableExecutorservice is mainly used to pack the existing thread pool. The thread pool after packaging cannot be modified, which is equivalent to final. The function signature is as follows:

public static ExecutorService unconfigurableExecutorService(ExecutorService executor)

unconfigurableScheduledExecutorService

UnconfigurableScheDexEcutorservice is used to pack the thread pool for packaging that can execute tasks. The thread pool after packaging cannot be modified, which is equivalent to final. The function signature is as follows:

public static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor)

defaultThreadFactory

DefaultThreadFactory method Return to the default factory method class. The default factory method is named the newly created thread in the thread pool:

POOL- [Virtual Machine Middleline Pond Number] -Thread- [thread number]

function signature is as follows:

public static ThreadFactory defaultThreadFactory()

source

Random Posts

problems and solutions of the high degree of collapse

CTFSHOW back serialization

vue use skills, and problems encountered

Ubuntu optimization

c#, enter the grades of any students, and use bubbling sorting to achieve sorting from small to small. In