I. Overview
Located inJava. util. concurrent package.
TheExecutor
,ExecutorService
,ScheduledExecutorService
,ThreadFactory
AndCallable
Class factory and practical methods. This class supports the following methods:
Create and return the string with common configurations
ExecutorService
. Create and return the string with common configurations
ScheduledExecutorService
. Create and return the "encapsulated" ExecutorService method, which disables reconfiguration by making implementation-specific methods inaccessible. Create and return
ThreadFactory
It can set the newly created thread to a known state. Create and return non-closure form
Callable
To use Callable. Ii. Detailed Methods
1. public static ExecutorServiceNewFixedThreadPool(Int nThreads) creates a thread pool that can reuse a fixed number of threads and runs these threads in a shared unbounded queue.
In any sense, in mostNThreadsThe thread is in the active state of the processing task. If an additional task is submitted when all threads are active, the additional task will wait in the queue before there is an available thread. 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 ). ExplicitlyClose
Previously, the thread in the pool will always exist.
Parameters:nThreads
-Number of threads in the pool
Return Value:Newly created thread pool
Throw:IllegalArgumentException
-IfNThreads <= 0
2. public static ExecutorServiceNewFixedThreadPool(Int nThreads, ThreadFactory threadFactory) creates a thread pool that can reuse a fixed number of threads and runs these threads in a shared, unbounded queue mode. When necessary, use the provided ThreadFactory to create new threads.
In any sense, in mostNThreadsThe thread is in the active state of the processing task. If an additional task is submitted when all threads are active, the additional task will wait in the queue before there is an available thread. 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 ). ExplicitlyClose
Previously, the thread in the pool will always exist.
Parameters:
nThreads
-Number of threads in the pool
threadFactory
-Factory used to create a new thread
Return Value:Newly created thread pool
Throw:
NullPointerException
-If threadFactory is null
IllegalArgumentException
-If NThreads <= 0
3. public static ExecutorServiceNewSingleThreadExecutor()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. Equivalent to otherNewFixedThreadPool (1)Different, you can use other threads without re-configuring the execution program returned by this method.
Return Value:Newly created single-thread Executor
4. public static ExecutorServiceNewSingleThreadExecutor(ThreadFactory threadFactory) creates an Executor that uses a single worker thread, runs this thread in an unbounded queue mode, and uses the provided ThreadFactory to create a new thread as needed.
Equivalent to otherNewFixedThreadPool (1, threadFactory)Different, you can use other threads without re-configuring the execution program returned by this method.
Parameters:
threadFactory
-Factory used to create a new thread
Return Value:Newly created single-thread Executor
Throw:
NullPointerException
-If threadFactory is null
5. public static ExecutorServiceNewCachedThreadPool() Create a thread pool that can create new threads as needed, but reuse them when previously constructed threads are available.
For programs that execute many short-term asynchronous tasks, these thread pools can usually improve program performance. CallExecuteThe previously constructed thread will be reused (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 useThreadPoolExecutor
Constructor creates a thread pool with similar properties but different details (such as timeout parameters.
Return Value:Newly created thread pool
6. public static ExecutorServiceNewCachedThreadPool(ThreadFactory threadFactory)
Create a thread pool that can create new threads as needed, but reuse the previously constructed threads when they are available, and use the provided ThreadFactory to create new threads as needed.
Parameters:threadFactory
-Factory used to create a new thread
Return Value:Newly created thread pool
Throw:NullPointerException
-If threadFactory is null
7. public static ScheduledExecutorServiceNewSingleThreadScheduledExecutor() Create a single-threaded execution program, which 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. Equivalent to otherNewScheduledThreadPool (1)Different, you can use other threads without re-configuring the execution program returned by this method.
Return Value:Newly created scheduled execution Program
8. public static ScheduledExecutorServiceNewSingleThreadScheduledExecutor(ThreadFactory 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. Equivalent to otherNewScheduledThreadPool (1, threadFactory)Different, you can use other threads without re-configuring the execution program returned by this method.
Parameters:threadFactory
-Factory used to create a new thread
Return Value:Newly created scheduled execution Program
Throw:
NullPointerException
-If threadFactory is null
9. public static ScheduledExecutorServiceNewScheduledThreadPool(Int corePoolSize) to create a thread pool, which can be scheduled to run commands after a given delay or regularly run.
Parameters:
corePoolSize
-The number of threads stored in the pool, even if the thread is idle.
Return Value:Newly created thread pool
Throw:
NullPointerException
-If threadFactory is null
10. public static ScheduledExecutorServiceNewScheduledThreadPool(Int corePoolSize, ThreadFactory threadFactory)
Create a thread pool that can be scheduled to run commands or periodically after a given delay.
Parameters:corePoolSize
-The number of threads stored in the pool, even if the thread is idle
threadFactory
-The factory used by the execution program to create a new thread
Return Value:Newly created thread pool
Throw:IllegalArgumentException
-IfCorePoolSize <0
NullPointerException
-If threadFactory is null
11. public static ExecutorServiceUnmarshableexecutorservice(ExecutorService executor)
ReturnsExecutorService
The method is delegated to the object of the specified execution program, but other methods may not be accessible when forced conversion is used. This provides a way to safely "freeze" configurations and not allow you to adjust specific implementations.
Parameters:executor
-Underlying implementation
Return Value:OneExecutorServiceInstance
Throw:NullPointerException
-If executor is null
12. public static ScheduledExecutorServiceUnmarshablescheduledexecutorservice(ScheduledExecutorService executor)
ReturnsExecutorService
The method is delegated to the object of the specified execution program, but other methods may not be accessible when forced conversion is used. This provides a way to safely "freeze" configurations and not allow you to adjust specific implementations.
Parameters:
executor
-Underlying implementation
Return Value:One ScheduledExecutorServiceInstance
Throw:
NullPointerException
-If executor is null
13. public static ThreadFactoryDefaultThreadFactory() Returns the default thread factory used to create a new thread.
This factory creates the sameThreadGroup
All new threads used by Executor. IfSecurityManager
, It usesSystem.getSecurityManager()
Group to call thisDefaultThreadFactoryMethod. In other cases, thread groups are used. Each new thread is created as a non-daemon and hasThread. NORM_PRIORITYAnd the maximum priority allowed in the thread group. The new thread hasPool-N-thread-MOfThread.getName()
The name of the access, whereNIs the serial number of the factory,MIs the serial number of the thread created in this factory.
Return Value:Thread factory
14. public static ThreadFactoryPrivilegedThreadFactory() Return the thread factory used to create new threads. These new threads have the same permissions as the current thread.
This factory is createddefaultThreadFactory()
For threads with the same settings, other settings of AccessControlContext and contextClassLoader of the new threadPrivilegedThreadFactoryThe thread of the method is the same. You canAccessController.doPrivileged(java.security.PrivilegedAction)
Create a newPrivilegedThreadFactory, Set the access control context of the current thread to create a thread with the selected permissions maintained in this operation.
Note that although Tasks running in such threads have the same access control and class loaders as the current thread, they do not need to have the sameThreadLocal
OrInheritableThreadLocal
Value. UseThreadPoolExecutor.beforeExecute(java.lang.Thread, java.lang.Runnable)
InThreadPoolExecutor
Before running any task in the subclass, you can set or reset the specific value of the local variable of the thread. In addition, if the worker thread must be initialized to have the same InheritableThreadLocal setting as some other specified threads, you can create a custom ThreadFactory in the environment where the thread waits and the service creates the request, instead of inheriting its value.
Return Value:Thread factory
Throw:
AccessControlException
-If the current access control context does not have the permission to obtain or set the context class loader.
15. public static Callable Callable(Runnable task, T result)
ReturnCallable
Object that can run a given task and return the given results. This requiresCallableMethod is useful when applied to other non-result operations.
Parameters:task
-Task to run
result
-Returned results
Return Value:A callable object
Throw: NullPointerException
-If the task is null
16. public static Callable Callable(Runnable task)
ReturnCallable
Object that can run a given task and returnNull.
Parameters:task
-Task to run
Return Value:A callable object
Throw:NullPointerException
-If the task is null
17. public static Callable Callable(PrivilegedAction Action)
ReturnCallable
An object that can be called to run an operation with a given privilege and return its results.
Parameters:action
-Privileged operations to run
Return Value:A callable object
Throw:NullPointerException
-If action is null
18. public static Callable Callable(PrivilegedExceptionAction Action)
ReturnCallable
An object that can be called to run abnormal operations with the given privileges and return the results.
Parameters: action
-Privileged abnormal operations to be run
Return Value:A callable object
Throw: NullPointerException
-If action is null
19. public static Callable PrivilegedCallable(Callable Callable)
ReturnCallable
Object, which can be called to execute the givenCallableObject. GenerallyAccessController.doPrivileged(java.security.PrivilegedAction)
This method is called in the operation to create a callable object. If possible, this object is executed under the selected permission settings maintained in this operation. If this method cannot be called, the relatedAccessControlException
.
Parameters: callable
-Underlying task
Return Value:A callable object
Throw: NullPointerException
-If callable is null
20. public static Callable PrivilegedCallableUsingCurrentClassLoader(Callable Callable)
ReturnCallable
Object. You can use the current context class loader as the context class loader to execute the givenCallableObject. GenerallyAccessController.doPrivileged(java.security.PrivilegedAction)
This method is called in the operation to create a callable object. If possible, this object is executed under the selected permission settings maintained in this operation. If this method cannot be called, the relatedAccessControlException
.
Parameters: callable
-Underlying task
Return Value:A callable object
Throw: NullPointerException
-If callable is null
AccessControlException
-If the current access control context does not have the permission to set or obtain the context class loader.