Executor framework complete interpretation

1 Introduction

Java thread is both a unit of work, but also enforcement mechanisms. Of JDK 5, the job execution unit is separated from the mechanism. And a working unit comprising a Runnable Callable, performed by the Executor framework mechanisms.

In the threading model of the HotSpot VM, Java threads are mapped to one local operating system threads. It will create a local operating system thread when the Java thread starts; when the Java thread terminates, the operating system thread can also be recycled. The operating system thread scheduling and assign them to all available CPU. In the upper layer, Java multithreaded programs usually applied into several tasks, and user-level scheduler (the Executor frame) maps these tasks for a fixed number of threads; in the underlying operating system kernel maps these threads to hardware processing on the device. The upper layer applications is controlled by the scheduler Executor frame; and the underlying operating system kernel is controlled by the scheduling, the scheduling not lower control program applications.

2 Executor framework consisting of

Executor frame consists of three major components as follows:

  • ① task: Runnableor Callableinterface and its implementation class

  • ② task executor: the main Executorand extension of Executor ExecutorServiceinterface to achieve some kind. There are two important Executor frame implementation class, a thread pool is an actuator ThreadPoolExecutor, the other is a timing task executors ScheduledThreadPoolExecutor.

  • The results ③ tasks: Futureinterface and its default implementationFutureTask

Description:

Runnable interface (no return value) and Callable interfaces (return value) implementation class may be ThreadPoolExecutoror ScheduledThreadPoolExecutorexecuted.

Executor is an interface, which is the basis Executor framework, submit mission will be separated from the task. ThreadPoolExecutorIt is the core of the implementation class thread pool, to perform tasks are submitted. ScheduledThreadPoolExecutorIs an implementation, the command can be run after a given delay, or to execute periodically. ScheduledThreadPoolExecutor more flexible than Timer, more powerful. Future Future interfaces of interface and implementation FutureTask class that represents the asynchronous task results.

3 Runnable和Callable

Callable Runnable interface implementation class and interface, or may be performed ThreadPoolExecutor ScheduledThreadPoolExecutor. The difference between them is Runnable no return value, it can not determine whether the task is completed, and there is a return Callable results.

In addition to creating your own object that implements the Callable interface, you can also use a factory class Executors will Runnable packed into a Callable.

callable(Runnable )The method does not require packaging results Runnable task, the task is completed Future.get()will get a null value; and callable(Runnable,T)may result in the need to package a Runnable task, the results can be specified by the parameter result, the task is completed Future.get()can obtain this result result.

    public static Callable<Object> callable(Runnable task) 
    public static <T> Callable<T> callable(Runnable task, T result)

 

4 ThreadPoolExecutor

ThreadPoolExecutorIs the ExecutorServicemost important implementation class, ThreadPoolExecutordo not directly implement ExecutorServicethe interface, it is directly inherited from AbstractExecutorServicethe abstract class, AbstractExecutorServicefor ExecutorSerivicesome of the interface methods did default implementation.

 

 Previous article thread pool ThreadPoolExecutor Introduction to create a thread pool using the constructor method having been described in detail, presented here using Executors factory class to create a thread pool. ThreadPoolExecutor can use the factory class Executors provides static factory methods, this can be easily configured to create some common thread pool. Executors can create three types of ThreadPoolExecutor.

1) fixed thread pool

newFixedThreadPoolSeries ways to create a thread pool fixed number of threads. It is suitable to meet the needs of resource management, and the need to limit the number of threads of the current scenario, it is suitable for heavier server load.

    // create a fixed number of threads in thread pool 
 public  static ExecutorService newFixedThreadPool ( int nThreads) {
     return  new new the ThreadPoolExecutor (nThreads, nThreads, 0L , TimeUnit.MILLISECONDS,
             new new a LinkedBlockingQueue <the Runnable> ());
  }
 public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>(), threadFactory);
  }

corePoolSize maximumPoolSize this thread pool and are set to parameters nThreads. When the number of threads in the thread pool greater than corePoolSize, keepAliveTime non-core thread is waiting for a new task the maximum time, these threads after more than this time will be terminated. corePoolSize and maximumPoolSize parameter set to the same value, indicating that all threads in the pool are the core thread, then keepAliveTime parameter is meaningless.

 

 Processing tasks Process Description:

① When the number of threads in the thread pool is less than corePoolSize, thread pool creates a new thread to perform the task.

② after a period of time to warm up, the number of threads reaching corePoolSize ( since the same maximumPoolSize and corePoolSize, this time also reached the maximum number of threads, the future will not create threads ), will begin the task into the work queue.

③ After that there is a new task to reach into the work queue ( LinkedBlockingQueue constructor with no arguments, queue capacity created is Integer.MAX_VALUE, this is almost impossible queue full capacity, the task will not refuse, reject the policy does not work ), if thread is free to obtain from the work queue and execute tasks.

2) single-threaded Pool

newSingleThreadExecutorSeries ways to create a single thread pool thread to ensure that it is suitable for the task execution order, at any time and at most only one active (executing) tasks.

  // create a single thread pool thread 
  public  static ExecutorService newSingleThreadExecutor (ThreadFactory threadFactory) {
     return  new new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,  new LinkedBlockingQueue<Runnable>(),threadFactory));
  }
  public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,  0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));
  }

newSingleThreadExecutor method is not a direct return ThreadPoolExecutor object, it will be packaged into FinalizableDelegatedExecutorService ThreadPoolExecutor target object, but the actual business process or entrusted to ThreadPoolExecutor to achieve.

corePoolSize and maximumPoolSize such thread pool are set to 1, there is at most one thread in the thread pool, the same place where keepAliveTime argument is meaningless, it uses an infinite capacity queue blocking task as storage container.

 

 Processing tasks Process Description:

① When no thread pool thread, the thread pool will create a thread to perform the task.

② When there is a thread pool thread, start the task into the work queue.

③ After that there is a new task to reach into the work queue ( LinkedBlockingQueue constructor with no arguments, queue capacity created is Integer.MAX_VALUE, this is almost impossible queue full capacity, the task will not refuse, reject the policy does not work ), If the only thread in the pool is idle removed from the work queue and execute tasks.

3) buffers thread pool

newCachedThreadPool series method based on the need to create a new thread pool thread ( the thread previously created but if available, the multiplex these threads ). It does not limit the number of threads, demand will create a new thread for the implementation of many of the short-term induction small tasks task, or lightly loaded servers.

    // The need to create a new thread to thread pool 
  public  static ExecutorService newCachedThreadPool (a ThreadFactory threadFactory) {
     return  new new the ThreadPoolExecutor (0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new new SynchronousQueue <the Runnable> (), threadFactory);
  }
  public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
  }

corePoolSize this thread pool is zero, maximumPoolSize as Integer.MAX_VALUE, show that there is no core thread pool threads, all threads are non-core thread, and may allow the maximum number of threads is almost infinite. keepAliveTime parameter is set to 60, indicating that the idle thread waits up to 60 seconds is terminated.

SynchronousQueue used here as a work queue, the queue is no capacity ( when trying to line up, just exactly idle thread is waiting for an interview will be the task team success ), but the maximum number of threads that can be created is infinite. This means that the speed of the main thread submit the task is greater than the task of processing speed, the thread pool will continue to create build a new thread, this may lead to too many threads created, the system resources are exhausted, the program crashes.

 

 Processing tasks Process Description:

① because the number of kernel threads is zero, so when you start the core thread pool thread pool has been filled. When the main thread to submit the first task, the thread pool will be attempting this task into the team because of SynchronousQueuethe special nature of this case only when the idle threads are also a team, both the team and the team exactly matches the main thread will tasks to idle thread to execute. Otherwise it will enter the next step.

② When the thread pool without any thread or no idle thread, the thread will not execute the operations team. At this point the thread pool thread will be created to build a new mission

③ step on a new thread created after executing the task, the task will wait for a call SynchronousQueue.poll team. The idle thread waits up to 60 seconds, if the main thread has submitted a new task in 60 seconds, this idle thread will get to this task and execute it. If you wait until 60 seconds, even before the new task arrives, this thread will be terminated.

5 ScheduledThreadPoolExecutor

ScheduledThreadPoolExecutorIt is a timing task execution, a construction method that can be created can also be created by the static method Executors factory class.

ScheduledThreadPoolExecutorConstructor logic is very simple, they call the parent class implementation directly.

public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
          new DelayedWorkQueue());
}
public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,new DelayedWorkQueue(), threadFactory);
}
public ScheduledThreadPoolExecutor(int corePoolSize,  RejectedExecutionHandler handler) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,  new DelayedWorkQueue(), handler);
}
public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue(), threadFactory, handler);
} 

 

Executors can create two types ScheduledThreadPoolExecutor.

①newSingleThreadScheduledExecutor series method for creating a scheduled task is a single thread of execution. It is suitable for performing a periodic timing of the task in a fixed order, and cases up performing a task simultaneously.

②newScheduledThreadPool series method is used to create a given number of threads timing task executor. It is suitable for tasks require multiple threads of execution cycles, while also limiting the number of threads to create a thread prevent excessive drain on resources.

   // threaded timing task executor 
     public  static The ScheduledExecutorService newSingleThreadScheduledExecutor (a ThreadFactory threadFactory) {
         return  new new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1, threadFactory));
    }
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
    // multithreaded timing task executor 
     public  static The ScheduledExecutorService newScheduledThreadPool ( int corePoolSize) {
         return  new new a ScheduledThreadPoolExecutor (corePoolSize);
    }
    public static ScheduledExecutorService newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }

 

ScheduledThreadPoolExecutor inherited from ThreadPoolExecutor, and implements the interface ScheduledExecutorService indicating the timing of the actuator. It is mainly used to run tasks after a given delay, or recurring tasks.

 

 DelayedWorkQueue ScheduledThreadPoolExecutor inner class is a static, which is unbounded queue, so the parent ThreadPoolExecutor maximumPoolSize, keepAliveTime meaningless these two parameters, has no effect.

Perform tasks basic process:

① When calling ScheduledThreadPoolExecutor of scheduleAtFixedRate () method or scheduleWithFixedDelay () method, add DelayedWorkQueue will to achieve a ScheduledFutureTask of a type of task RunnableScheduledFuture interface.

② thread pool thread attempts to acquire from DelayedWorkQueue expire task, if no tasks that are due this thread will block until to get a real job, and then perform this task.

 

 ScheduledThreadPoolExecutor ScheduledFutureTask task will be scheduled into a DelayedWorkQueue, we take a look ScheduledFutureTask.

 ScheduledFutureTask is a member of the inner class ScheduledThreadPoolExecutor, which inherited the FutureTask class, and also to achieve a periodic task ScheduledFutureTask represent the interface, there are three important ScheduledFutureTask member variables.

  • long-type member variable time, it indicates that the specific time of the task to be performed.

  • long-type member variable sequenceNumber, indicates that the task is added to the number ScheduledThreadPoolExecutor.

  • long-type member variable period, indicate the cycle interval of task execution

DelayedWorkQueue encapsulates a RunnableScheduledFuture array, which uses the array to achieve a priority queue based on a heap sort, which is similar to the principle of PriorityQueue. ScheduledFutureTask task into this array, DelayedWorkQueue will have an array of tasks sorted by priority. The basic principle is sort of: time low standing in the front, if the same time two tasks, it is more sequenceNumber, sequenceNumber small top surface. In other words, the first time as early as the task execution, if several tasks equally early, to see who should submit, to submit the first task execution.

  Perform a task complete these steps:

① an idle thread gets ScheduledFutureTask task has expired (from DelayedWorkQueue the expiration tasks are ScheduledFutureTask of time greater than equal to the current time ).

② ScheduledFutureTask this thread perform this task.

③ modify this thread ScheduledFutureTask of time variable for the next time it will be executed.

④ This thread this time ScheduledFutureTask after being modified back into DelayedWorkQueue in.

6 Future

Future interface class is used to represent the results and FutureTask asynchronous tasks. When we Runnable interface or class that implements Callable interface submitted ( using ExecutorServicethe submitseries method to submit the task ) when to ThreadPoolExecutor or ScheduledThreadPoolExecutor, they will return an Futureobject type ( actual return FutureTasktype of the object ), calling Future.get()upon method blocks waiting task is completed and then returns the results of the task.

submitMethod to submit the task

    public Future<?> submit(Runnable task) 
    public <T> Future<T> submit(Runnable task, T result)
    public <T> Future<T> submit(Callable<T> task)

 

getMethod waits to perform tasks and get complete results

 public V get() throws InterruptedException, ExecutionException
 public V get(long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException

 

Future, a detailed analysis of FutureTask in the previous article thoroughly interpret FutureTask source has indicated, will not repeat them here.

Reference: "Concurrent Programming in the Java Art" , "the Java logic" 

 

 

Guess you like

Origin www.cnblogs.com/gocode/p/fully-interpretate-executor-framework.html