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,