Part 1: java. util. concurrent-Runnable Vs Callable in Java

Source: Internet
Author: User

About Runnable:
Runnable interface is implemented by the Thread class as well and it's a common protocol for all the objects who wish to execute in a different thread. it's one of the ways of creating threads in Java. the other way to create a thread is by subclassing the Thread class. A class implementing Runnable interface can simply pass itself to create a Thread instance and can run thereafter. this eliminates the need of subclassing the Thread class for the purpose of executing the code in a separate thread.
As long as we don't wish to override other methods of the Thread class, it may be a better idea to implement the Runnable interface to enable multithreading capabilities to a class than enabling the same by extending the Thread class.

About Callable:
The designers of Java felt a need of extending the capabilities of the Runnable interface, but they didn 'twant to affect the uses of the Runnable interface and probably that was the reason why they went for having a separate interface named Callable in Java 1.5 than changing the already existing Runnable interface which has been a part of Java since Java 1.0.

Similarities:
1) Both threads can be used to cause a separate stack for a thread.

2) Both has only one method: Inside Runnable it is called
Public abstract void run ();
Inside callable the method is being called
Public abstract call ();
(Note: V can be any valid object in JAVA)

Differences:

1) Return Type-
Public abstract void run () --> Return type "void"
Public abstract call () --> Return type "Any valid JAVA Object"

2) Run method can not throw any checked Exception, whereas
Public abstract call () throw checkedException
Call method can throw any checked Exception.

Package com. jovialjava. blog. threads;

Import java. util. concurrent. Callable;
Import java. util. concurrent. FutureTask;

/**
* This program has been written to demonstrate the usage
* Of Runnable and Callable <String> Interfaces. Jovial Java makes
* No guarantee of any harm or any problem caused by using this
* Program.
*/
Public class Run_Vs_Call {


Public static void main (Stringargs ){
CallableTask call = new CallableTask ();
RunnableTask run = new RunnableTask ();
Try {
/*
* We need a Future task Launcher to launch Callable Class
*/
FutureTask <String> callTask = new FutureTask <String> (call );
/*
* We need a Thread Launcher to launch Runnable Class
*/
Thread runTask = new Thread (run );
CallTask. run ();
RunTask. start ();

/*
* Get is a blocking call, JVM will wait here until Call task
* Finishes execution.
*/
System. out. println (callTask. get ());


} Catch (Exception e ){
E. printStackTrace ();
}

}

/*
* Callable <V> is defined as Callable <String>
*
*/
Public static class CallableTask implements Callable <String> {

Public String call (){
System. out. println ("Inside call method ..!! ");
Return "hello ";
}
}
/*
* Runnable Interface Implementation
*/
Public static class RunnableTask implements Runnable {
Public void run (){
System. out. println ("Inside Run Method, I can not return any thing ");
}
}

}
Author: Yun silence is Jin Jing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.