Java Multithreaded-executor Framework Overview

Source: Internet
Author: User

1. Task-related interfaces and classes1.1 Runnable

Represents a command that can be executed, typically used to perform a task in a different thread.

package java.lang;publicinterface Runnable {    publicvoidrun();}
1.2 callable<v>

Represents a task with a return result

package java.util.concurrent;publicinterface Callable<V> {    V call() throws Exception;}
1.3 future<v>

Represents the result of an asynchronous task.

 PackageJava.util.concurrent;public interface Future<v> {/** * Cancels the execution of the task. * The following scenarios do not cancel the task successfully: * 1. The task is complete. 2. The task has been canceled.     3. The task cannot be canceled for some reason.     * * If successful, the task will not be executed.     * If the task has been started, the parameter mayinterruptifrunning will determine if the execution task thread should be interrupted and the purpose of stopping the task has been reached.     * * After the method returns, IsDone () will always return true.     * Returns True if the method returns True,iscanelled (). */Boolean Cancel (Boolean mayinterruptifrunning);/** * Returns true to indicate that the task was canceled before completion. */Boolean iscancelled ();/** * Returns true to indicate task completion * * Task completion possible for the following reasons: * 1. Normal execution complete 2. Exception 3. Canceled */Boolean IsDone ();/** * Waits for the task to complete and returns the result. * * @return return Task Execution results * @throws cancellationexception When the task is canceled * @throws Executionexc Eption when the task execution occurs * @throws interruptedexception The thread is interrupted during the wait process * /V get ()throwsInterruptedexception, executionexception;/** * Waits for the task to complete within a specified time and returns the result. * * @param wait time to timeout * @param Time unit * @return return Task Execution result * @throws Cancell Ationexception when the task is canceled * @throws executionexception When the task execution occurs * @throws interruptedexception Wait * @throws timeoutexception Wait Timeout * * When the thread is interrupted in the processV get (long timeout, timeunit unit)throwsInterruptedexception, Executionexception, TimeoutException;}
1.4 Runnablefuture<v>

This interface inherits from Runnable and future.

package java.util.concurrent;publicinterface RunnableFuture<V> extends Runnable, Future<V> {    void run();}
1.5 futuretask<v>

An asynchronous task that can be canceled. This class implements the Runnablefuture interface.

2. Executor Framework2.1 Executor

Executor is responsible for performing the submitted runnable task. This interface provides a mechanism to decouple task submissions from task execution. Executor are typically used instead of display creation threads.

package java.util.concurrent;publicinterface Executor {    void execute(Runnable command);}
2.2 Executorservice

The interface inherits from the executor interface. Provides a way to manage the claims cycle and a convenient way to submit tasks.

 Public  interface executorservice extends Executor {    voidShutdown (); List<runnable> Shutdownnow ();BooleanIsShutDown ();BooleanIsterminated ();BooleanAwaittermination (LongTimeout, timeunit unit)throwsInterruptedexception;    <T> future<t> Submit (callable<t> Task);    <T> future<t> Submit (Runnable task, T result);    Future<?> Submit (Runnable Task); <T> list<future<t>> InvokeAll (collection<? extends callable<t>> tasks)throwsInterruptedexception; <T> list<future<t>> InvokeAll (collection<? extends callable<t>> tasks,LongTimeout, timeunit unit)throwsInterruptedexception; <T> T invokeany (collection<? extends callable<t>> tasks)throwsInterruptedexception, executionexception; <T> T invokeany (collection<? extends callable<t>> tasks,LongTimeout, timeunit unit)throwsInterruptedexception, Executionexception, TimeoutException;}
2.3 Executors

Executors is a static factory for the production of executor, Executorservice, Scheduledexecutorservice, Threadfactory and callable.

 PackageJava.util.concurrent; Public  class executors {     Public StaticExecutorserviceNewfixedthreadpool(intNthreads) {return NewThreadpoolexecutor (Nthreads, Nthreads,0L, Timeunit.milliseconds,NewLinkedblockingqueue<runnable> ()); } Public StaticExecutorserviceNewsinglethreadexecutor() {return NewFinalizabledelegatedexecutorservice (NewThreadpoolexecutor (1,1,0L, Timeunit.milliseconds,NewLinkedblockingqueue<runnable> ())); } Public StaticExecutorserviceNewcachedthreadpool() {return NewThreadpoolexecutor (0, Integer.max_value, -L, Timeunit.seconds,NewSynchronousqueue<runnable> ()); } Public StaticScheduledexecutorserviceNewsinglethreadscheduledexecutor() {return NewDelegatedscheduledexecutorservice (NewScheduledthreadpoolexecutor (1)); } Public StaticScheduledexecutorserviceNewscheduledthreadpool(intCorepoolsize) {return NewScheduledthreadpoolexecutor (corepoolsize); } Public StaticThreadfactorydefaultthreadfactory() {return NewDefaultthreadfactory (); } Public StaticThreadfactoryprivilegedthreadfactory() {return NewPrivilegedthreadfactory (); } Public Static<T> callable<t>callable(Runnable task, T result) {if(Task = =NULL)Throw NewNullPointerException ();return NewRunnableadapter<t> (task, result); } Public StaticCallable<object>callable(Runnable Task) {if(Task = =NULL)Throw NewNullPointerException ();return NewRunnableadapter<object> (Task,NULL); }/** cannot instantiate. * *    Private Executors() {}}
3. UML Class Diagram

Copyright NOTICE: This article is the original blogger article, reprint please indicate the source http://blog.csdn.net/sun927

Java Multithreaded-executor Framework Overview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.