submit (): This method receives the Runnable or callable object as an input parameter (this method inputs parameters refer to the previous Article Java thread Pool task submission (Runnable, callable, Futuretask). This article mainly talks about the return value of the Submit method and the use of the return value, and returns a future object. The call method in the task submitted by the Submit method returns an Integer, then the Submit method returns future<integer>; if the call method returns float, then the Submit method returns future< Float>;call method returns the List<integer>, then the Submit method returns FUTURE<LIST<INTEGER>> The return value of the Submit method is referenced using a reference of the corresponding type, depending on which one is returned. For example:
The call method returns a type of List<integer>, so the Submit method returns a value type of future<list<integer>>.
The results of the above sample program output are as follows:
Use of the return value of the Submit method, as we already know, the actual return value is list<integer>, but is encapsulated as FUTURE<LIST<INTEGER>> we can pass the Get () method to remove the List<integer> object from the inside. In the previous article (Java Thread Pool task submission (Runnable, callable, Futuretask) ) We have explained that the Submit method is actually pseudo return (Retrospective: The Submit method, once executed, will immediately produce the return value of the future type, and the task just submitted may be queued in the task queue on the online pool). So in this case, when we call the Get () method on the future object returned by the Submit method to take out the inside list<integer>, the thread that the Get () method is in will block (blocking the call when the Get () method of the future Class). Until the task submitted by submit is actually executed in the pool and the result is produced.