java- concurrent programming Executor, Executors, ExecutorService

Executor

Brief introduction

Executor frame is introduced in Java 5, the internal mechanism using a thread pool, which at java.util.cocurrent packet start threads controlled by the frame, and the closed execution, concurrent programming operation can be simplified. Thus, after the Java 5, to start threads better than by the Executor Thread start method, in addition to a more manageable, more efficiency (implemented thread pool, save costs), the

As well as the key point: this helps to avoid the escape - if we start a thread in the constructor, because another task may begin before the end of the constructor, this time may have access to half of the object initialization Executor used in the constructor. Eexecutor as a flexible and powerful asynchronous execution framework, which supports many different types of task execution strategy, provides a standard way to perform the task of the submission process and the development process of decoupling, based on producer - consumer model, submitted the task is equivalent to the producer thread, a thread to perform tasks equivalent to the consumer, and is represented by Runnable task, Executor's implementation also provides support for life-cycle, and statistics collection, management mechanism and application performance monitoring mechanisms.

Executor: an interface, which defines a receiving method executor Runnable object, the method signature executor (Runnable Command), the method receives a Runable example, which is used to perform a task, the task that is a class that implements Runnable interface, in general, Runnable task to open up to use in the new thread is:. new thread (new RunnableTask ())) start (), but in the Executor, the Executor can be used without having to explicitly create threads: executor.execute (new RunnableTask ()); Source follows:

UML class diagram Executor

ExecutorService

Brief introduction

  • Executor is a more widespread use than sub-class interface, which provides a method of life cycle management, returned Future object, and can track one or more asynchronous tasks Future of return method; ExecutorService can call the shutdown () method ExecutorService closed smoothly, after the method is called, would lead to ExecutorService stop accepting any new tasks and wait for task execution has been submitted to complete (the task has been submitted will be divided into two categories: one is already in progress, and the other is also It does not begin execution), when the task is finished all that have been submitted will be closed ExecutorService. So we generally use this interface to implement and manage multi-threading.
  • By ExecutorService.submit () Future method returns an object, you can call isDone () method to query whether the Future has been completed. When the task is completed, it has a result, you can call the get () method to obtain the result. You can not isDone () checks directly call the get () get results, in this case, get () will block until the results are ready, you can cancel the execution of the task. Future provides a cancel () method is used to cancel the task of execution pending. ExecutorService all the code (except Note) code is as follows:

Executors

Brief introduction

Executors class provides factory methods for creating a series of thread pool, thread pool returns have achieved ExecutorService interface.

Mainly in the following 6 created:

newCachedThreadPool

  • Cache-type pool, first check the thread pool has no previously established, and if so, on reuse. If not, build a new thread to join the pool
  • Buffer pool typically used to perform some type of survival of the asynchronous task is very short and therefore not by much in the daemon of some type SERVER with connection-oriented. But for the short term survival asynchronous task, it is the first choice of Executor.
  • Can reuse the threads in the thread pool must be within the timeout IDLE, the default timeout is 60s, long beyond this IDLE, thread instance will be terminated and removed from the pool.
  •   Note that threads into CachedThreadPool have to worry about its end, more than TIMEOUT inactivity, it will automatically be terminated.

newFixedThreadPool

  • newFixedThreadPool and cacheThreadPool almost as well as be able to reuse it with, but not ready to build a new thread
  • Its uniqueness: at any point in time, can only have a fixed number of active threads exist at this time if there is a new thread to be established, only on another waiting in the queue until a thread terminates the current thread directly It was removed from the pond
  • And cacheThreadPool different, FixedThreadPool no IDLE mechanism (may have, but since the document did not mention, must be very long, similar to depend on top of TCP or UDP IDLE mechanism and the like), so FixedThreadPool majority against some very stable and very fixed regular concurrent threads, used for server
  • Viewed from source method, cache pool and fixed pool called pool bottom is the same, but different parameters:
  • The number of fixed pool of threads fixed, and is 0 seconds IDLE (no IDLE), the number of threads cache pool support 0-Integer.MAX_VALUE (apparently did not fully consider the resource capacity of the host), 60 seconds IDLE 

newScheduledThreadPool

  • Scheduling type thread pool
  • This pool of threads can be executed in turn delay schedule, or cycle execution

newWorkStealingPool

  • Work stealing, using multiple queues to reduce competition (ForkJoinPool achieve)

newSingleThreadExecutor

  • Singleton thread, only one thread at any time in the pool
  • With the fixed pool and the pool and cache the same underlying pool, but the number of threads is 1-1,0 seconds IDLE (no IDLE)
  • Only use a single thread to perform the task, even if submitted more tasks, are also waiting in the queue will be put to wait

newSingleThreadScheduledExecutor

  • Delayed execution can be single-threaded, the timing of execution thread pool

Executor VS  ExecutorService VS Executors区别

  • Executor and ExecutorService first difference is: ExecutorService interface extends Executor interface, sub-interface is the Executor
  • Executor and second difference is ExecutorService: Executor interface defines the execute()method for receiving anRunnableobject interface, the interface ExecutorService submit()acceptable methodsRunnableandCallableobject interface.
  • Executor and third difference is ExecutorService: Executor the execute() method does not return any results, and ExecutorService the submit()method can return a Future object calculation results.
  • Executor and fourth difference is ExecutorService: In addition to allowing the client to submit a task, ExecutorService also provides a method for controlling the thread pool. For example: calling shutDown() method to terminate the thread pool.
  • Executors class provides factory methods to create different types of thread pool. For example: newSingleThreadExecutor () creates a thread only a thread pool, newFixedThreadPool (int numOfThreads) to create a fixed number of thread pool threads, newCachedThreadPool () can create new threads as needed, but if there is idle thread will reuse the there are threads.

Make small but daily progress! ! !

Guess you like

Origin www.cnblogs.com/woxbwo/p/11462770.html