[Link].
com
info@[Link]
Creating Threads in Java
1st Floor, Laxmi Tower, Bhaskar Circle, Ratanada, Jodhpur.
Development – Training - Placement - Outsourcing
Creating Threads in Java
There are two ways to create a thread in Java:
1) By extending Thread class.
2) By implementing Runnable interface.
2
[Link]
How we create thread?
Class Interface Thread class provide constructors and methods
to create and perform operations on a
Object Runnable [Link] class extends Object class and
implements Runnable interface.
The Runnable interface should be implemented
by any class whose instances are intended to be
Thread executed by a thread. Runnable interface have
only one method named run().
public void run(): is used to perform action
for a thread.
3
[Link]
Definition of Thread Class
Thread is a class in [Link] package. The Thread class
extends an Object class, and it implements Runnable Class Interface
interfaces. The Thread class has constructors and
methods to create and operate on the thread. When we Object Runnable
create multiple threads, each thread creates a unique
object and get associated with that object. If you create
a thread extending Thread class, further you can not
extend any other class as java does not support multiple
inheritance. Thread
So, you should choose to extend Thread class only when
you also want to override some other methods of
Thread class.
4
[Link]
Thread creation by extending the Thread class
We create a class that extends the [Link] class. This class
overrides the run() method available in the Thread class. A thread
begins its life inside run() method. We create an object of our new class
and call start() method to start the execution of a thread. Start()
invokes the run() method on the Thread object.
5
[Link]
Example
class test extends Thread{ Output :
public void run(){
[Link]("thread is running"); thread is running
}
public static void main(String args[]){
test t1=new test();
[Link]();
Starting a thread:
} start() method of Thread class is used to start a
} newly created thread. The thread moves from New
state to the Runnable state. When the thread gets a
chance to execute, its target run() method will run.
6
[Link]
Definition of Runnable Interface
Runnable is an interface in [Link] package.
Class Interface
Implementing Runnable interface we can define a
thread. Runnable interface has a single method
run(), which is implemented by the class that
Object Runnable
implements Runnable interface. When you choose
to define thread implementing a Runnable
interface you still have a choice to extend any
other class. When you create multiple threads by Thread
implementing Runnable interface, each thread
shares the same runnable instance.
7
[Link]
Thread creation by implementing the Runnable Interface
We create a new class which implements [Link] interface
and override run() method. Then we instantiate a Thread object and
call start() method on this object.
8
[Link]
Example
class test implements Runnable{
public void run(){
Output:
[Link]("thread is running");
} thread is running
public static void main(String args[]){
test t=new test();
Thread t1 =new Thread(t); If you are not extending the Thread class, your class
object would not be treated as a thread object. So
[Link](); you need to explicitely create Thread class object.
} We are passing the object of your class that
implements Runnable so that your class run()
} method may execute.
9
[Link]
Differences Between Thread and Runnable
a) Each thread created by extending the Thread class creates a unique object for
it and get associated with that object. On the other hand, each thread created
by implementing a Runnable interface share the same runnable instance.
b) As each thread is associated with a unique object when created by extending
Thread class, more memory is required. On the other hand, each thread
created by implementing Runnable interface shares same object space hence,
it requires less memory.
c) If you extend the Thread class then further, you can inherit any other class as
Java do not allow multiple inheritance whereas, implementing Runnable still
provide a chance for a class to inherit any other class.
10
[Link]
Differences Between Thread and Runnable
a) One must extend a Thread class only if it has to override or specialise some
other methods of Thread class. You must implement a Runnable interface if
you only want to specialise run method only.
b) Extending the Thread class introduces tight coupling in the code as the code of
Thread and job of thread is contained by the same class. On the other hand,
Implementing Runnable interface introduces loose coupling in the code as the
code of Thread is seprate from the job assigned to the thread.
11
[Link]
Conclusion
It is preferred to implement a Runnable interface instead of extending
Thread class. As implementing Runnable makes your code loosely
coupled as the code of thread is different from the class that assign job
to the thread. It requires less memory and also allows a class to inherit
any other class.
12
[Link]
[Link]
info@[Link]
Thank You
[Link] [Link]
[Link] [Link]
[Link]
1st Floor, Laxmi Tower, Bhaskar Circle, Ratanada, Jodhpur.
Development – Training - Placement - Outsourcing