JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label ExecutorService. Show all posts
Showing posts with label ExecutorService. Show all posts

Java ExecutorService example using Callable and Future

8:21 AM
Callable interface represents a thread that can return a value. It is very much similar to Runnable interface except that it can return a value. Callable interface can be used to compute status or results that can be returned to invoking thread. For example: Let’s say you want to perform factorial and square of some numbers, you can do it concurrently using callable interface which will return value too. Callable defines only one method as below 1 2 3 4 5 6   public interface Callable { V call ( ) throws Exception ;   }   You can define the task which you want to perform inside call method. If it executes successfully, call method will return the result else it must throw an exception. You can use ExecutorService’s submit to execute Callable task. Let’s see Signature of submit method in ExecutorService. 1 2 3      Future submit ( Callable task ) ;   If you notice, return

.