Cracking Core Java Interviews 3rd Edition

A Comprehensive Guide to Crack Core Java Interviews in Investment Banks, HealthCare IT & Startups. It covers Core Java, Algorithms, Data Structures, Concurrency, Hibernate and Spring MVC.

Specifically for investment banking domain, healthcare IT and product companies i.e. UBS, RBS, Blackrock, Morgan Stanley, JP Morgan, Nomura, Barclays, Citibank, Markit, Bank of America, Goldman Sachs and other companies i.e. Global Logic, Adobe, hCentive, Edifecs, Expedia, Infosys, TCS, Sapient, Wipro, HCL etc.
Free Chapters PDF
Buy Full PDF ₹250
3rd Edition
Last Updated : Tuesday, December 29, 2015 9:30:59 AM IST Total Page Hits 858

What is difference between Callable and Runnable Interface?

As per Java documentation

“Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.”

So basically there are two main differences between the two -

  1. A Runnable does not return a result
  2. A Runnable can’t throw checked Exception

Both of these interfaces can be used with Executors framework introduced in Java 5.

public interface Callable { 
 V call() throws Exception; 

In order to convert Runnable to Callable use the following utility method provided by Executors class

Callable callable = Executors.callable(Runnable task); 

Callable, however must be executed using a ExecutorService instead of Thread as shown below.

result = exec.submit(aCallable).get(); 

Submitting a callable to ExecutorService returns Future Object which represents the lifecycle of a task and provides methods to check if the task has been completed or cancelled, retrieve the results and cancel the task.

Here is the source for Future Interface

public interface Future { 
  boolean cancel(boolean mayInterruptIfRunning); 
  boolean isCancelled(); 
  boolean isDone(); 
  V get() throws InterruptedException, ExecutionException; 
  V get(long timeout, TimeUnit unit) 
  throws InterruptedException, ExecutionException, TimeoutException;

What is need for both the Interfaces (callable & Runnable)?

In my opinion - In theory, the Java team could have changed the signature of the method, but this would have broken binary compatibility with pre 1.5 Java code, requiring re-coding when migrating old Java code to newer JVMs. That is a BIG NO-NO. Java strives to be backwards compatible … and that’s been one of Java’s biggest selling points for business computing.

Similar Articles

1. Synechron Java Interview Questions

Collection of Java Interview Questions (Core Java, Spring, database and other concepts) for Synechron in banking and finance domain

2. Design Metro Smart Card System for Delhi using Java

Design a program in Java for Metro Smart Card System in Delhi. Evaluation criteria will be based on code completeness, code structure and quality, modularity, usage of OO principles, choice of data structure and unit tests.

3. What does volatile keyword do in a multi-threading environment

volatile keyword helps programmers write thread safe program