Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 788
نمايش فراداده

Executorsjava.util.concurrent

Java 5.0

This utility class defines static factory methods for creating ExecutorService and ScheduledExecutorService objects. Each of the factory methods has a variant that allows you to explicitly specify a ThreadFactory. newSingleThreadExecutor( ) returns an ExecutorService that uses a single thread and an unbounded queue of waiting tasks. newFixedThreadPool( ) returns an ExecutorService that uses a thread pool with the specified number of threads and an unbounded queue. newCachedThreadPool( ) returns an ExecutorService that does not queue tasks but instead creates as many threads as are needed. When a task terminates, its thread is cached for reuse. Cached threads are allowed to terminate if they remain unused for 60 seconds.

newSingleThreadScheduledExecutor( ) returns a ScheduledExecutorService that uses a single thread for running tasks. newScheduledThreadPool( ) returns a ScheduledExecutorService that uses a thread pool of the specified size.

The factory methods of this class typically return instances of ThreadPoolExecutor and ScheduledThreadPoolExecutor. If the returned objects are cast to these implementing types, they can be configured (to change the thread pool size, for example). If you want to prevent this from happening, use the unconfigurableExecutorService( ) and unconfigurableScheduledExecutorService( ) methods to obtain wrapper objects that implement only the ExecutorService and ScheduledExecutorService methods and do not permit configuration.

Other methods of this class include callable( ), which returns a Callable object wrapped around a Runnable and an optional result, and defaultThreadFactory( ), which returns a basic ThreadFactory object. Executors also define methods related to access control and the Java security system. A variant of the callable( ) method wraps a Callable around a java.security.PrivilegedAction. privilegedCallable( ) is intended to be invoked from within a PrivilegedAction being run with AccessController.doPrivileged( ). When passed a Callable in this way, it returns a new Callable that can be used later to invoke the original callable in a privileged access control context, granting it permissions that it would not otherwise have.

public class

Executors { // No Constructor // Public Class Methods public static Callable<Object>

callable (java.security.PrivilegedAction

action ); public static Callable<Object>

callable (Runnable

task ); public static Callable<Object>

callable (java.security.PrivilegedExceptionAction

action ); public static <T> Callable<T>

callable (Runnable

task , T

result ); public static ThreadFactory

defaultThreadFactory ( ); public static ExecutorService

newCachedThreadPool ( ); public static ExecutorService

newCachedThreadPool (ThreadFactory

threadFactory ); public static ExecutorService

newFixedThreadPool (int

nThreads ); public static ExecutorService

newFixedThreadPool (int

nThreads , ThreadFactory

threadFactory ); public static ScheduledExecutorService

newScheduledThreadPool (int

corePoolSize ); public static ScheduledExecutorService

newScheduledThreadPool (int

corePoolSize , ThreadFactory

threadFactory ); public static ExecutorService

newSingleThreadExecutor ( ); public static ExecutorService

newSingleThreadExecutor (ThreadFactory

threadFactory ); public static ScheduledExecutorService

newSingleThreadScheduledExecutor ( ); public static ScheduledExecutorService

newSingleThreadScheduledExecutor (ThreadFactory

threadFactory ); public static <T> Callable<T>

privilegedCallable (Callable<T>

callable ); public static <T> Callable<T>

privilegedCallableUsingCurrentClassLoader (Callable<T>

callable ); public static ThreadFactory

privilegedThreadFactory ( ); public static ExecutorService

unconfigurableExecutorService (ExecutorService

executor ); public static ScheduledExecutorService

unconfigurableScheduledExecutorService (ScheduledExecutorService

executor ); }