Java thread pool related class-executor framework

Source: Internet
Author: User

1.Executor

Interface Source code:

Public interface Executor {    /**     * Executes the given command at some time on the future.  The command     * May execute in a new thread, in a pooled thread, or in the calling     * thread, at the discretion of th e <tt>Executor</tt> implementation.     *     * @param command the runnable task     * @throws Rejectedexecutionexception If this task cannot be     * accepted fo R execution.     * @throws nullpointerexception if command is null     *    /void Execute (Runnable command);}

Interface using:

 public  class   T01_myexecutor implements Executor { public  static  void   main (string[] args) { new  t01_myexecutor () . Execute (()->system. Out . println ( " hello executor   "  public  void   execute (Runnable command) { //  Command.run (); }}

2.ExecutorService

Source:

Recognize the Submit method, expand the Execute method, have a return value

<T> future<t> Submit (callable<t> Task); <T> future<t> Submit (Runnable task, T result); Future <?> Submit (Runnable Task);

Use:

Executorservice provides a way to manage the eecutor life cycle, and the life cycle of Executorservice includes the following: Running off and terminating three states.  Executorservice is running when initialization is created. The shutdown method waits for the submitted task to complete and no longer accepts new tasks, and closing the Shutdownnow method after completing all committed tasks forces the terminating of all running tasks and no longer allows the submission of new tasks A runnable or callable can be submitted to Executorservice's Submit method to execute, and eventually return to a futire used to get the execution result of the task or cancel the task (after completion of the task and return to the execution result)
 Public classCallableandfuture { Public Static voidMain (string[] args) throws Executionexception, interruptedexception {executorservice executor=Executors.newsinglethreadexecutor (); Future<String> future = Executor.submit (NewCallable<string> () {//accept a callable instance  PublicString Call () throws Exception {return "Mobin";        }        }); System. out. println ("execution results of the task:"+future.Get()); }}
Output:
Execution result of the task: Mobin

3.Executors

Provides a series of static factory methods for creating various thread pools

Newfixedthreadpool: Creates a reusable thread pool that has a fixed number of threads, and if all threads in the thread pools are active, then commits the task to wait in the queue until there are available threads, and if a thread in the thread pool ends up with an exception, a new thread is added to it.
Scheduledexecutorservice executor = Executors.newscheduledthreadpool (5);

4.ThreadPool

Concept of thread pool

 Public Static voidMain (string[] args) throws interruptedexception {Executorservice service= Executors.newfixedthreadpool (5);//Execute Submit         for(inti =0; I <6; i++) {Service.execute ()- {                Try{TimeUnit.MILLISECONDS.sleep ( -); } Catch(interruptedexception e) {e.printstacktrace (); } System. out. println (Thread.CurrentThread (). GetName ());        }); } System. out. println (service);        Service.shutdown (); System. out. println (service.isterminated ()); System. out. println (Service.isshutdown ()); System. out. println (service); TimeUnit.SECONDS.sleep (5); System. out. println (service.isterminated ()); System. out. println (Service.isshutdown ()); System. out. println (service); }

5.Future

 Public Static voidMain (string[] args) throws Interruptedexception, executionexception {futuretask<Integer> task =NewFuturetask<> ((){TimeUnit.MILLISECONDS.sleep ( -); return  +; }); //New Callable () {Integer call ();}                NewThread (Task). Start (); System. out. println (Task.Get());//Blocking//*******************************Executorservice Service = Executors.newfixedthreadpool (5); Future<Integer> f = service.submit ((){TimeUnit.MILLISECONDS.sleep ( -); return 1;        }); System. out. println (F.Get()); System. out. println (F.isdone ()); }

6.

Java thread pool related class-executor framework

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.