Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
151 views

Creating Threads in Java

Creating threads in Java can be done in two ways: by extending the Thread class or implementing the Runnable interface. Extending Thread creates a unique object for each thread but uses more memory, while implementing Runnable shares the same object between threads and uses less memory. It is preferred to implement Runnable to avoid tight coupling and allow a class to inherit other classes.

Uploaded by

Manish WsCube
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Creating Threads in Java

Creating threads in Java can be done in two ways: by extending the Thread class or implementing the Runnable interface. Extending Thread creates a unique object for each thread but uses more memory, while implementing Runnable shares the same object between threads and uses less memory. It is preferred to implement Runnable to avoid tight coupling and allow a class to inherit other classes.

Uploaded by

Manish WsCube
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

www.wscubetech.

com
info@wscubetech.com

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
www.wscubetech.com
How we create thread?
Class Interface Thread class provide constructors and methods
to create and perform operations on a
Object Runnable thread.Thread 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
www.wscubetech.com
Definition of Thread Class

Thread is a class in java.lang 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
www.wscubetech.com
Thread creation by extending the Thread class

We create a class that extends the java.lang.Thread 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
www.wscubetech.com
Example
class test extends Thread{ Output :
public void run(){
System.out.println("thread is running"); thread is running
}
public static void main(String args[]){
test t1=new test();
t1.start();
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
www.wscubetech.com
Definition of Runnable Interface

Runnable is an interface in java.lang 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
www.wscubetech.com
Thread creation by implementing the Runnable Interface

We create a new class which implements java.lang.Runnable interface


and override run() method. Then we instantiate a Thread object and
call start() method on this object.

8
www.wscubetech.com
Example

class test implements Runnable{


public void run(){
Output:
System.out.println("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
t1.start(); 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
www.wscubetech.com
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
www.wscubetech.com
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
www.wscubetech.com
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
www.wscubetech.com
www.wscubetech.com
info@wscubetech.com

Thank You
https://www.facebook.com/wscubetech.india https://www.linkedin.com/company/wscube-tech
https://plus.google.com/+wscubetechjodhpur https://www.youtube.com/c/wscubetechjodhpur

https://twitter.com/wscube

1st Floor, Laxmi Tower, Bhaskar Circle, Ratanada, Jodhpur.

Development – Training - Placement - Outsourcing

You might also like