Java Create Threads
2. Creating a thread by implementing Runnable interface
Now, we write another program, with the same functionality (printing 0 to 9 with an interval of 1000 milliseconds), but implementing Runnable interface instead of the earlier Thread class.
public class Demo implements Runnable
{
public void run()
{
try
{
for(int i=0; i<10; i++)
{
System.out.println("Welcome to threads: " + i);
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.err.println("Sleep time is disturbed, you may get incorrect functionality. " + e);
}
System.out.println("Successful");
}
public static void main(String args[])
{
Demo d1 = new Demo();
Thread t1 = new Thread(d1);
t1.start();
}
}
In the above program, the object, d1, is not implicitly a thread because it is not extending Thread class; but implements Runnable interface. It takes one more statement to get the run() method executed.
Demo d1 = new Demo();
Thread t1 = new Thread(d1);
t1.start();
Create an object, t1, of Thread class and pass the d1 object as parameter. Then, start the thread t1 with start() method. It indirectly calls the run() method of Demo class. This is extra work when Runnable interface is implemented instead of extending Thread class.
Java Create Threads: Extends vs Implements
When to use Thread or when to prefer Runnable. Even though no difference exists in the output, there is a reason and necessity to implement Runnable interface in some cases. If your class already extends another class like Frame or Applet, you cannot extend Thread class as Java does not support multiple inheritance. To overcome this problem, designers introduced another style of implementing Runnable interface. If your class does not extend another class, you can go for Thread class.
Java Create Threads: Why implementing Runnable approach is better than Thread extending?
In general, not exactly pertaining threads, when a method is called by an object, the JRE checks for overloaded methods exist or not, super class method is overridden or not (especially when a subclass object is assigned to super class object or reference variable) etc. It may be the same case with run() method of Thread class. If you implement Runnnable, JRE checks run() is implemented or not but does not check all the other round about rules. For this reason, everyone says implementing Runnable is speedier than Thread extends.
what is the Main difference between extending Thread class and implementing Runnable interface. Apart from Multiple inheritance.
sleep() is inherited from Runnable interface?
sleep() method of Thread class. Runnable interface contains only one method – run().
Awesome explaination
Thank you
Sir can you please tell me the callable interfaces in java
What you mean by callable interfaces? Is it CallableStatement in JDBC? Be clear please.
Sir,
Why your write class thread extends object implements Runnable. Even we say once a class does not extends any class it has default super class that is object. Or it has different meaning. I am not getting
Java API classes writes like that only, even though JVM extends Object implicitly.
Simple but very very clear about the introduction of threads..
sir ,what is the need of passing runnable interface object as an argument in new Thread();??
Runnable interface contains method run(). If you pass an object of a class that implements Runnable interface, the thread object implicitly calls the run() method of the class. This is adjustment, the designers planned to call the callback method run(). To achieve this exactly, if you pass an object that does not implements Runnable interface, it is an error.
The same thing you can observe with throw keyword. You must throw any object that must be a Throwable or subclass of Throwable.
what is the Main difference between extending Thread class and implementing Runnable interface. Apart from Multiple inheritance.
Sir plz post more example programs on multi threading and packages also
This is site is meant for learners. What I gave it enough for a novice. Refer forums.
Sir we are defining run() method but we are calling start() method with object what is this meaning and in place of start() method i placed run() method also it is working can u plz elaborate it??
What you say is one of the good examples to prove that Java is a robust language. If you call start() method, it calls implicitly run() method. If you call run() directly, certain activities that JVM should perform on the thread would not be done. For example, when thread finishes its job, it will be garbage collected or when you call join() method, the parent thread should wait until the child dies. So, call start() instead of run().
Many examples exist in Java like this.
Respected Rao Sir,
Please read my answer and guide me if I am worng:
When we call start() method.. the call of the run() method is done in the code of start() method. When we call run() method instead of start() in order to run the thread it will be called but it will not be done in a separate thread. In order to call in the separate stack we should call start() method..
Thank You
Abhishek.
By calling start() method many internals of Thread manipulations is done by the Java designer.
sir ,
Why we need to create a Thread in two ways . ?
Generally we create thread by extending Thread class. But if you already extend another class, then there is no possibility of extending Thread class as Java does not support multiple inheritance. To overcome this difficulty, there comes implementing Runnable interface.