Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Differences » Difference between Runnable and Callable in Java

  • Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा
    Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा Festival
  • JSON The Data Interchange Standard Format
    JSON The Data Interchange Standard Format Important
  • What is Internship? How to get Internship?
    What is Internship and how to get Internship? Important
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
    DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार Important
  • What is Array in Java? Types of array in Java
    What is Array in Java? Types of array in Java Java
  • HTTP status codes List | Response Status Code Glossary
    HTTP status codes List | Response Status Code Glossary Important
  • Linked List in Data Structure and Algorithm, Types, Operations
    Linked List in Data Structure and Algorithm, Types, Operations C Language
  • Function in C Language | Recursive function with example
    Function in C Language | Recursive function with example C Language

Difference between Runnable and Callable in Java

Posted on March 22, 2022May 4, 2022 By admin No Comments on Difference between Runnable and Callable in Java
Difference between Runnable and Callable in Java

Runnable and Callable are both useful for implementing multithreading. Runnable has been enhanced into Callable in Java 1.5. It’s important to understand that Tasks that will be executed by other threads are encapsulated using the Callable and Runnable interfaces.

The java.util.concurrent package has a Callable. Runnable is available in java.lang.Thread. There are, however, some differences between these interfaces.

What is Callable in Java? Callable is a Java interface that allows you to run many processes in parallel.

Table of Contents

  • Runnable Vs Callable interface in Java
  • How to convert Runnable to Callable?
  • Example of Runnable interface in java
  • Example of Callable interface in java
  • Interview Questions and Answers Runnable Vs Callable

Runnable Vs Callable interface in Java

When using Executor Framework, the topic of Runnable vs Callable arises. The key difference between Runnable and Callable is that Callable returns to the caller the outcome of running a job. You can’t return anything if you use Runnable.

#CallableRunnable
1.Callable was first introduced in Java 1.5.Runnable was first introduced in Java 1.0.
2.Callable has return type it means Callable can return value.Runnable does not return value.
3.Callable has call() method.Runnable has run() method.
4.Callable throws CheckedException.Runnable cannot throw CheckedException.
5.Callable is a generic interface.But Runnable is not a generic interface.
6.public interface Callable<V> {
V call() throws Exception;
}
public interface Runnable {
void run();
}
7.The Callable interface cannot be passed to a thread as a parameter to create a new thread.But we can pass Runnable to a thread as a parameter to create a new thread.

We can only use Callable in the Executor Framework. However, Runnable may be handed to a single thread for execution as well as the Executor framework.

How to convert Runnable to Callable?

You can use the Executor class’s utility method for conversion.

Runnable runnable = ...;
Callable callable = Executors.callable(runnable);

Note: If you’re familiar with Executors, Callable is a better choice than Runnable.

Example of Runnable interface in java

You may start a new thread with Runnable by either implementing the Runnable interface or subclassing the Thread class. The Runnable interface is demonstrated in the example below.


public class RunnableExample implements Runnable {
	public void run() {
		System.out.println("Runnable Example!");
	}

	public static void main(String args[]) {
		RunnableExample r = new RunnableExample();
		Thread t = new Thread(r);
		t.start();
	}
}

Example of Callable interface in java

The Callable interface is demonstrated in the example below.


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

class CallableExample implements Callable {

	@Override
	public String call() throws Exception {
		System.out.println("In Call Method");
		String name = "This is the example of Callable";
		return name;
	}
}

public class CallableDemo {
	public static void main(String args[]) throws Exception {
		ExecutorService executorService = Executors.newSingleThreadExecutor();
		Future f = executorService.submit(new CallableExample());
		System.out.println("Note: " + f.get());
	}
}

See the difference between Runnable and Thread

Interview Questions and Answers Runnable Vs Callable

When should you use a Runnable and when should you use a Callable?

If you have tasks that you want to schedule and forget, use Runnable. Use a Callable if you want to get a value from a method.

Is Runnable a functional interface?

Yes, Runnable is a function interface.

What is the limitation of Callable in Java?

The Thread class does not have a method that accepts the Callable interface.

Can Runnable throw exception?

No

Is it true that Callable creates a Thread?

No, It is not true. We can’t create a thread with a Callable.

Is Callable a functional interface?

Yes, Callable is a functional interface.

What is the use of Callable interface in Java?

Use the Callable interface to encapsulate a task and send it to a thread or thread pool for asynchronous execution.

How can I return value from Runnable?

You can’t return any value from Runnable. Runnable returns void type.

Can we use Callable instead of runnable?

Yes

Which is best thread or runnable?

Runnable is recommended.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Differences, Important, Java Tags:Can we use Callable instead of runnable?, Why do we need Callable in Java?, Why is Callable used?

Post navigation

Previous Post: HTTP status codes List | Response Status Code Glossary
Next Post: Java 11 new Features (With example Programs)

More Related Articles

Difference between Internet and Intranet Difference between Internet and Intranet Differences
What is Insurance with full details? What is Insurance with full details? | Life, Health, Car Insurance Important
Visual Paradigm Online and integration with Visual Studio Visual Paradigm Online and integration with Visual Studio Important
What is Mutual Fund? What is Mutual Fund? Important
Operational Qualification Operational Qualification (OQ) Important
Java Comments Java Comments : Types of java comments, Syntax & Examples Java

Related Posts

  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • What is Insurance with full details?
    What is Insurance with full details? | Life, Health, Car Insurance Important
  • Visual Paradigm Online and integration with Visual Studio
    Visual Paradigm Online and integration with Visual Studio Important
  • What is Mutual Fund?
    What is Mutual Fund? Important
  • Operational Qualification
    Operational Qualification (OQ) Important
  • Java Comments
    Java Comments : Types of java comments, Syntax & Examples Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
  • Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा
    Sawan ka Mahina : सावन का महीना का महत्व, भगवान शिव की पूजा Festival
  • JSON The Data Interchange Standard Format
    JSON The Data Interchange Standard Format Important
  • What is Internship? How to get Internship?
    What is Internship and how to get Internship? Important
  • DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार
    DBMS in Hindi | DBMS क्या है? | DBMS की विशेषताएं और प्रकार Important
  • What is Array in Java? Types of array in Java
    What is Array in Java? Types of array in Java Java
  • HTTP status codes List | Response Status Code Glossary
    HTTP status codes List | Response Status Code Glossary Important
  • Linked List in Data Structure and Algorithm, Types, Operations
    Linked List in Data Structure and Algorithm, Types, Operations C Language
  • Function in C Language | Recursive function with example
    Function in C Language | Recursive function with example C Language
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved