Square CSS

Showing posts with label executorservice. Show all posts
Showing posts with label executorservice. Show all posts

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 ... 

Saturday, November 17, 2018

Java Multithreading : ExecutorService

Executor Service is an interface in Java to create a pool of threads or worker threads to execute a task asynchronously. The instance of ExecutorService is created and with the help of its instance, a pool of threads are created. The thread pool can have single or multiple threads depending upon the requirement.

Creation of thread pool :


A thread pool can be created with the help of ExecutorService in the below-mentioned way.
  • Creation of Single thread by using newSingleThreadExecutor().
  • Creation of multiple threads by using newFixedThreadPool(n) where n is the required count of the worker threads.
  • Creation of catched thread pool by using newCachedThreadPool().
       

Syntax is mentioned below :




ExecutorService execute=Executors.newSingleThreadExecutor();    

   /* For single thread creation */

ExecutorService execute=Executors.newFixedThreadPool();
  
   /* For a fixed count of worker threads */

ExecutorService execute=Executors.newCachedThreadPool();
   
   /* For catched thread pool */







                    Submit() and execute() are the methods of ExecutorService in Java. One ... 

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