ways of creating Threads in Java Program
Multi threaded applications are the base of all modern computing applications.Threads save lot of time and keeps utilizing computing resources to the optimum levels.It is important for a Java developer to understand how to create Threads.
There are two ways by which you can create Threads:-
1.By extending Thread class
2.By implementing Runnable Interface
Creating Threads by extending Thread class
The java.lang.Thread class is used to construct and access individual threads in a multi threaded application.Thread class supports many methods that obtain information about the activities of a Thread,set and check properties of a Thread and cause a Thread to wait,be interrupted or be destroyed.Extending the Thread class enables your class to run in a separate new call stack.Just like the main Thread.
Example:-
class MThread extends Thread{
// code goes here
}
Creating Threads by Implementing Runnable Interface
Extending Thread class for creating Threads is no doubt a bad OO Practice because since Java doesn't support multiple Inheritance,if you extend one class and also need to incorporate features of Other class then what you are supposed to do?
Let us Consider a scenario where we want to create an Applet which has some Thread capabilities,now if you extend Thread then from where applet methods come from and if you extend Applet class then from where the Thread capabilities come from.This is the reason why the Java creators decided to give some extra flexibility by providing us with Runnable Interface which consists of only a single method called run().All the Thread related code goes inside run() method and Thread run() method is invoked when we call start() method on Thread object.This run() method starts a new call stack in this case but it has to be invoked to get things working.
Example:-
class MThread implements Runnable{
public void run(){
//Thread stuff goes here
}
}
NOTE:-Consider the context of Applet and Thread scenario ,when you use Runnable interface,the Thread becomes a part of the Applet and grants full access to its data members and methods.The subclass of Thread is limited to access the public components of any class.Therefore ,when the Thread depends strongly on the components of Applet class use Runnable Interface.Use this concept in general terms not only for Applet context.
Suggested Reading:-
Java Thread Basics
Thread Life Cycle
Read more...
There are two ways by which you can create Threads:-
1.By extending Thread class
2.By implementing Runnable Interface
Creating Threads by extending Thread class
The java.lang.Thread class is used to construct and access individual threads in a multi threaded application.Thread class supports many methods that obtain information about the activities of a Thread,set and check properties of a Thread and cause a Thread to wait,be interrupted or be destroyed.Extending the Thread class enables your class to run in a separate new call stack.Just like the main Thread.
Example:-
class MThread extends Thread{
// code goes here
}
Creating Threads by Implementing Runnable Interface
Extending Thread class for creating Threads is no doubt a bad OO Practice because since Java doesn't support multiple Inheritance,if you extend one class and also need to incorporate features of Other class then what you are supposed to do?
Let us Consider a scenario where we want to create an Applet which has some Thread capabilities,now if you extend Thread then from where applet methods come from and if you extend Applet class then from where the Thread capabilities come from.This is the reason why the Java creators decided to give some extra flexibility by providing us with Runnable Interface which consists of only a single method called run().All the Thread related code goes inside run() method and Thread run() method is invoked when we call start() method on Thread object.This run() method starts a new call stack in this case but it has to be invoked to get things working.
Example:-
class MThread implements Runnable{
public void run(){
//Thread stuff goes here
}
}
NOTE:-Consider the context of Applet and Thread scenario ,when you use Runnable interface,the Thread becomes a part of the Applet and grants full access to its data members and methods.The subclass of Thread is limited to access the public components of any class.Therefore ,when the Thread depends strongly on the components of Applet class use Runnable Interface.Use this concept in general terms not only for Applet context.
Suggested Reading:-
Java Thread Basics
Thread Life Cycle


















