Java concurrent Programming-Executor Framework (i) Executor,

Source: Internet
Author: User

1, a programming method of concurrent programming is to split the task into a few columns of small tasks, namely, runnable, and then submit these tasks to a Executor execution, executor.execute (runnalbe) . Executor uses its internal thread pool at execution time to complete the operation.

Executor sub-interfaces are: Executorservice,scheduledexecutorservice, known implementation class: Abstractexecutorservice, Scheduledthreadpoolexecutor,threadpoolexecutor.

2, executor belongs to the public type of interface. Can be used to submit, manage or perform runnable tasks. The class that implements the executor interface can also control the specifics of the runnable task execution thread. Includes details of thread usage, scheduling, and so on. In general, the runnable task is created in a new thread using: New (New (Runnabletask ())). Start ()

3. In executor, however, you can use executor instead of the display to create the thread. For example, you can use the following method to create a thread instead of calling new Thread (...) as a 2nd in each task in a task. The method.

Exectuor executor = Anexecutor (); Executor.execute (new Runnabletask ()); Asynchronously executes Executor.execute (new Runnabletask ());

4, however, the executor interface does not strictly require that execution must be asynchronous/synchronous, and everything is quite free. In the simplest case, the executing program can run the submitted task immediately in the caller's thread,

Class Directexecutor implements Executor {public void execute (Runnable r) {R.run ();}}

More commonly, the task is executed in a thread that is not the caller thread. If you start in another thread:

Class Threadpertaskexecutor implements Executor {public void execute (Runnable r) {new Thread (R). Start ();}}

You can also serialize the execution process using another executor in the implementation:

Class Serialexecutor implements Executor {final queue<runnable> tasks = new arraydeque<runnable> (); final Executor Executor; Runnable active; Serialexecutor (Executor Executor) {this.executor = Executor;} Public synchronized void Execute (final Runnable R) {Tasks.offer (new Runnable () {public void run () {try {r.run ();} finally {Schedulenext ();}}); if (active = = null) {Schedulenext ();}} protected synchronized void Schedulenext () {if ((active = Tasks.poll ()) = null) {Executor.execute (active);}}}

5. The Threadpoolexecutor class provides a scalable thread pool implementation. The Executors class provides a convenient factory approach to the executor interface and its implementation.

6. Method execute in Executor. void execute (Runnable command) indicates that a given command is executed at some time in the future. The command may be executed in a new thread, a thread that is already in the pool, or a thread that is being called.



Java concurrent Programming-Executor Framework (i) Executor,

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.