UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

I want to know what are the differences between runnable and thread class implementation except they are class and interface and how to decide which is better at what time ...

Asked by Last Modified  

21 Answers

Learn Java

Follow 0
Answer

Please enter your answer

Java/J2EE, B.E./B.Tech/MCA SubjectsTraining

1) Implementing Runnable is the preferred way to do it. Here, you’re not really specializing or modifying the thread’s behavior. You’re just giving the thread something to run. That means composition is the better way to go. 2) Java only supports single inheritance, so you can only extend one class. 3)...
read more
1) Implementing Runnable is the preferred way to do it. Here, you’re not really specializing or modifying the thread’s behavior. You’re just giving the thread something to run. That means composition is the better way to go. 2) Java only supports single inheritance, so you can only extend one class. 3) Instantiating an interface gives a cleaner separation between your code and the implementation of threads. 4) Implementing Runnable makes your class more flexible. If you extend thread then the action you’re doing is always going to be in a thread. However, if you extend Runnable it doesn’t have to be. You can run it in a thread, or pass it to some kind of executor service, or just pass it around as a task within a single threaded application. 5) By extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same runnable instance. read less
Comments

Industry expert and professional lecturer/trainer

Well it depends on your need. Generally you extend any class when either you want to over ride or use all or most of the methods and interface you want to implement the methods. In Thread class there are tons of methods but we override only one run() method and Runnable interface has only one method...
read more
Well it depends on your need. Generally you extend any class when either you want to over ride or use all or most of the methods and interface you want to implement the methods. In Thread class there are tons of methods but we override only one run() method and Runnable interface has only one method to implement that is run(). So its a performance benefit and common sens to use Runnable interface than Thread class. read less
Comments

IT Professional Trainer working with a reputed Institute. Headquarters: Hyderabad

Thread vs. Runnable in Java 1) Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java. 2) In Object oriented programming extending a class generally means...
read more
Thread vs. Runnable in Java 1) Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java. 2) In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead. 3) Runnable interface represent a Task which can be executed by either plain Thread or Executors or any other means. So logical separation of Task as Runnable than Thread is good design decision. 4) Separating task as Runnable means we can reuse the task and also has liberty to execute it from different means. Since you cannot restart a Thread once it completes. Again Runnable vs Thread for task, Runnable are winner. 5) Java designer recognizes this and that's why Executors accept Runnable as Task and they have worker thread which executes those task. read less
Comments

Java,J2EE,Spring,Webservices,Hibernate,Cloud And Android Expertise

Runnable is better if your class still wanted to extend or implement any other class. If your class is responsible only for thread activities and if you dont want to extend any other class then use Thread.
Comments

Here are some of my thoughts on whether I should use Thread or Runnable for implementing task in Java, though you have another choice as "Callable" for implementing thread which we will discuss later. 1) Java doesn't support multiple inheritance, which means you can only extend one class in Java so...
read more
Here are some of my thoughts on whether I should use Thread or Runnable for implementing task in Java, though you have another choice as "Callable" for implementing thread which we will discuss later. 1) Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java. 2) In Object oriented programming extending a class generally means adding new functionality, modifying or improving behaviors. If we are not making any modification on Thread than use Runnable interface instead. 3) Runnable interface represent a Task which can be executed by either plain Thread or Executors or any other means. so logical separation of Task as Runnable than Thread is good design decision. 4) Separating task as Runnable means we can reuse the task and also has liberty to execute it from different means. since you can not restart a Thread once it completes. again Runnable vs Thread for task, Runnable is winner. 5) Java designer recognizes this and that's why Executors accept Runnable as Task and they have worker thread which executes those task. 6) Inheriting all Thread methods are additional overhead just for representing a Task which can can be done easily with Runnable. These were some of notable difference between Thread and Runnable in Java, if you know any other differences on Thread vs Runnable than please share it via comments. I personally use Runnable over Thread for this scenario and recommends to use Runnable or Callable interface based on your requirement. read less
Comments

Trainer

If you dont have any parent class go for Thread . If you have any parent class then go for Runnable . Both are the same.
Comments

If we want to make a sub class as thread class, we must use Runnable interface instead of Thread class, because already the sub class is extends from another super class, hence it cannot extends another class (Thread). Multiple inheritance is not supported in java
Comments

explore the depth of 'C' language..

The difference is that when u r implementing an interface u will have to provide the bodies for all the functions inside the class in which u r implementing.
Comments

Java Trainer

Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance andcan not extend or inherit another class in Java. But by implementing Runnable Interface we can get a chance to extends one more class. By...
read more
Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance andcan not extend or inherit another class in Java. But by implementing Runnable Interface we can get a chance to extends one more class. By extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same runnable instance. read less
Comments

IT Professional Trainer with 15 years of experience in IT Industry

Thread vs Runnable in Java is always been a confusing decision for beginners in java. Thread in Java seems easy in comparison of Runnable because you just deal with one class java.lang. Thread while in case of using Runnable to implement Thread you need to deal with both Thread and Runnable two classes....
read more
Thread vs Runnable in Java is always been a confusing decision for beginners in java. Thread in Java seems easy in comparison of Runnable because you just deal with one class java.lang. Thread while in case of using Runnable to implement Thread you need to deal with both Thread and Runnable two classes. Though decision of using Runnable or Thread should be taken considering differences between Runnable and Thread and pros and cons of both approaches. This is also a very popular thread interview questions and most of interviewer are really interested to know what is your point of view while choosing Thread vs Runnable or opposite. In this java article we will try to point out some differences between Thread and Runnable in Java which will help you to take an informed decision. read less
Comments

View 19 more Answers

Related Questions

I know HTML, CSS, and a bit of JavaScript. What should I learn next?
HTML,CSS and Javascript are Tools which are used for Front-End Web Development. The next step is to learn the following: 1)Learn Javascript Frameworks like node.js,react.js,angular.js etc. 2)Learn Back-End...
Inch By
Need to learn java....
You may contact me. I teach Java in great depth and with very firm building blocks.
Ajay
What is serialization?
serialization is a storing the state of the object into the file
Shubham
How do I learn Java? From book or internet or a coaching?
It is good to learn Java from Coaching where you will get more exposure in learning at faster pace.
Suresh
0 0
7

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

JAVA Version History
Java Version History: There are many java versions that has been released. Current stable release of Java is Java SE 8. JDK Alpha and Beta (1995). JDK 1.0 (23rd Jan, 1996). JDK 1.1 (19th Feb, 1997). J2SE...
V


Differences Between HashMap vs HashSet In Java.
HashSet HashMap HashSet implements Set interface. HashMap implements Map interface. HashSet stores the data as objects. HashMap stores the data as key-value pairs. HashSet...

TestNG Annotations and its sequence
public class TestNGAnnotations { @BeforeMethod public void beforeM() { System.out.println("Before Method"); } @AfterMethod public void afterMethod() { System.out.println("After Method"); } @BeforeClass...
S

Sarthak C.

0 0
0

Create Immutable Class
Snippet of an Immutable Class: package com.stringhr; /*Declare the class as 'final'. This would prevent any other class from extending it and hence from overriding any method from it which could modify...

Recommended Articles

Java is the most commonly used popular programming language for the creation of web applications and platform today. Integrated Cloud Applications and Platform Services Oracle says, “Java developers worldwide has over 9 million and runs approximately 3 billion mobile phones”.  Right from its first implication as java 1.0...

Read full article >

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more