Square CSS

Sunday, November 18, 2018

Java Multithreading: ExecutorService methods Submit() & Execute()

Submit() and execute() are the methods of ExecutorService in Java. One can assign any task in these methods for execution of a particular code block.


The Future interface:


The Future interface is used to get the return data of Callable and Runnable tasks. It has a method called get() so one can pass the result of submit() in the Future instance to track the output result.  


Difference between submit() and execute():


Since both the methods can be used to perform a given task but major difference lies between the return type of both the method. The return type of execute() method is void so it returns null when data is passed in Future object.

So when execute() method is passed in Future then compile time error is shown.


But when submit() method is called no compile-time error is displayed and the code runs successfully.

One can track the execution of a code block in submit() method by getting the return data using get() method of future. But this is not possible in execute() method of ExecutorService as the return type of execute() method is void.



Types methods of submit() method:


There are two types of submit method. One with the Runnable and the other with the Callable interface.

  • submit(new Runnable): In which Runnable has the method public void run().
  • submit(new Callable): In which Callable has the method public Object call().


Difference between submit(Runnable) and submit(Callable) :


The return type is the main difference between the submit with Runnable and submit with Callable.
As the name suggests the Callable calls the data at the end and provides in the Future object.



As we can see Object is the return type in public Object call(). The object is the return type here and the "Got something in return with callable" got printed in the console.


But in the case of submit with Runnable the return type is void so "null" gets printed in the console


As one can see void is the return type here in public void run() so "null" is passed in the Future.


Refer the video tutorial for more detail:





                    Executor Service is an interface in Java to create a pool of the ... 

1 comment:

  1. The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Final Year Project Centers in Chennai.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete

Some Algorithms

Algorithm: Tower of Hanoi

Tower of Hanoi consists of three towers called as pegs with n number of rings. Rings are of different size.  Conditions to be fulfill...

Popular Posts