Then the thread pool, using the Java thread pool, the general situation of the thread pool corresponding to the implementation class is Threadpoolexecutor, of course, do not exclude themselves to write a thread pool, pull away, threadpoolexecutor Extends from the abstract class Abstractexecutorservice, where the default implementation is Abstractexecutorservice:
The default implementation method first let's look at the Submit () method we mentioned above:
Source:
PublicFuture<?>Submit (Runnable Task) {if(Task = =NULL)Throw NewNullPointerException (); Runnablefuture<Object> ftask = newtaskfor (Task,NULL); Execute (ftask); returnFtask; } Public<T> future<t>Submit (Runnable task, T result) {if(Task = =NULL)Throw NewNullPointerException (); Runnablefuture<T> Ftask =newtaskfor (task, result); Execute (ftask); returnFtask; } Public<T> future<t> Submit (callable<t>Task) { if(Task = =NULL)Throw NewNullPointerException (); Runnablefuture<T> Ftask =newtaskfor (Task); Execute (ftask); returnFtask; }
What you can see is:
The type of arguments that are accepted for the submit () is the class that implements the Runable or callable interface and is then encapsulated as runnablefuture<T> ftask = newtaskfor (Task); Given to execute (ftask), returns the result of execution. this execute (Runnable) method will be implemented in subclass Threadpoolexecutor.
Let's take a look at the description of the executor interface first:
Public InterfaceExecutor {/*** 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. * * @paramcommand The runnable task *@throwsRejectedexecutionexception If this task cannot is * accepted for execution. * @throwsnullpointerexception if command is null*/ voidExecute (Runnable command);}
Executing a given task command at some point in the future indicates that the method is going to be executed in the future and is not blocked in the main thread (the Submit method). Also, notice that if the Runnable interface is committed, the result of the returned future object is specified at the time of the task submission, either null or T result. The above analysis can be simply tested under:
// callable task class Callabeltask implements callable<string>{ String name; public Callabeltask (String name) {this . Name = name; } @Override public String call () thr OWS Exception {System.out.println ( "Start to execute the task" +
name); TimeUnit.SECONDS.sleep (
5 return name + "is done!" ; } }
Public Static voidSubmitcallabletask ()throwsexception{Executorservice Executor=Executors.newcachedthreadpool (); List<Future<String>> results =NewArraylist<future<string>> (5); for(inti = 0; I < 5; ) {Results.add (Executor.submit (NewCallabeltask ("Task_" + (+ +)i))) ; } System.out.println ("All of the tasks have been submited through InvokeAll method!"); Executor.shutdown (); for(future<string>f:results) System.out.println (F.get ()); }
Public Static void throws Exception {submitcallabletask ();} // result of calling Submitcallabletask: All the tasks has been submited through InvokeAll method! Start to execute the task Task_1start to execute the task Task_5start to execute the task Task_3start to execute the t Ask Task_4start to execute the task Task_2task_1was done! task_2is done! task_3is done! task_4is done! task_5is done!
From the returned results, the main thread does not block after executing executor.submit (new Callabeltask ("Task_" + (++i)), but continues to execute the PRINTLN statement to print out the statement:
All the tasks has been submited through InvokeAll method!
The submitted task will be executed by other threads at "some point in the future". Of course, in the main thread to get execution results f.get () must be blocked, because since we want to get results, of course, wait until the completion of the task to return only Ah!
If you change the above callabeltask to Runnabletask, the result of the return will be null, because the time of submission is specified in the new Taskfor method that the return result is null, which is the phrase in the submit source code above:
null);
The code for the test is:
classRunnabletaskImplementsrunnable{String name; PublicRunnabletask (String name) { This. Name =name; } @Override Public voidrun () {System.out.println ("Start to execute the task" +name); Try{TimeUnit.SECONDS.sleep (5); } Catch(interruptedexception e) {e.printstacktrace (); } } }//runnabletask Execution Results:All the tasks has been submited through InvokeAll method!Start to execute the task Task_1start to execute the task Task_5start to execute the task Task_3start to execute the t Ask Task_4start to execute the task Task_2NULLNULLNULLNULLNULL
Multithreading (iii)