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

  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
    Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी Biography
  • World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस
    World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस General Knowledge
  • Vedaant Madhavan Biography in Hindi, Family, School, Age
    Vedaant Madhavan Biography in Hindi, Family, School, Age Biography
  • States of Matter : Physical Properties of Matter (Solid, Liquid, Gas)
    States of Matter | Physical Properties of Matter (Solid, Liquid, Gas) Science
  • List Of National Animals Of Countries
    List Of National Animals Of Countries General Knowledge
  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
    MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi Biography
  • Farmers Day
    National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती General Knowledge
  • Newton's laws of Motion, State and Explained, Formula
    Newton’s laws of Motion, State and Explained, Formula Science

Difference between Runnable and Callable in Java

Posted on March 22, 2022May 4, 2022 By GeekCer Education 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

What is International Monetary Fund (I.M.F.) What is International Monetary Fund (I.M.F.)? Important
HTTP status codes List | Response Status Code Glossary HTTP status codes List | Response Status Code Glossary Important
ACID Properties in Hindi? ACID Properties क्या है? ACID Properties in Hindi? ACID Properties क्या है? Important
Lambda Expression in Java 8 | Functional Interface | Example Lambda Expression in Java 8 | Functional Interface | Example Java
Difference between Git Merge and Git Rebase | Git Rebase vs Git Merge Difference between Git Merge and Git Rebase | Git Rebase vs Git Merge Differences
Java Learning Tutorial Java Tutorial for Advanced Learning, Buzzwords, Parts of Java Java

Leave a Reply Cancel reply

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

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • 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