Executor framework refers to a series of functions related to executor in concurrent libraries introduced in java 5, including thread pool, Executor, Executors, Executor Service, Completion Service, Future, Callable, etc. Their relationship is: One way of programming concurrent programming is to break tasks into columns of small tasks, namely Runnable, and then submit them to an Executor for execution, Executor.execute(Runnalbe). Executor uses an internal thread pool to perform operations during execution. 1. Creating thread poolsThe Executors class provides a series of factory methods for pioneering thread pools, and the returned thread pools all implement the ExecutorService interface. public static ExecutorService newFixedThreadPool(int nThreads) Create a pool of threads with a fixed number of threads. public static ExecutorService newCachedThreadPool() Create a cacheable thread pool and call execute to reuse previously constructed threads if they are available. If no existing thread is available, create a new thread and add it to the pool. Terminate and remove threads that have not been used for 60 seconds from the cache. public static ExecutorService newSingleThreadExecutor() Create a single threaded Executor. public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) Create a thread pool that supports timed and periodic task execution, which in most cases can be used to replace the Timer class.
Executor Service and Life CycleExecutor Service extends Executor and adds some lifecycle management methods. An Executor's life cycle has three states: running, closing, and terminating. Executor was running when it was created. When ExecutorService.shutdown() is called, it is closed, and the isShutdown() method returns true. At this point, you shouldn't think about adding tasks to Executor. After all the added tasks have been executed, Executor is terminated and isTerminated() returns true. If Executor is closed, submitting a task to Executor throws unchecked exception Rejected Execution Exception.
3. Using Callable, Future returns the resultFuture < V > represents an asynchronous operation. The result of the operation can be obtained by the get() method. If the asynchronous operation has not been completed, get() will block the current thread. FutureTask < V > implements Future < V > and Runable < V >. Callable represents a return worth operation.
ExecutoreService provides a submit() method that passes a Callable, or Runnable, back to Future. If the Executor background thread pool has not completed the Callable calculation, the call returns the get() method of the Future object, blocking until the calculation is completed. Example: Parallel computing of the sum of arrays.
Main |
- int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 10, 11 };
- ConcurrentCalculator calc = new ConcurrentCalculator();
- Long sum = calc.sum(numbers);
- System.out.println(sum);
- calc.close();
Reproduced in: https://my.oschina.net/u/249393/blog/268066