In Java we can implement the thread programs using two approaches - Using Thread class, sing runnable interface.
Creating a Thread
In Java
we can implement the thread programs using two approaches -
o Using
Thread class
o sing
runnable interface.
As given
in Fig. 3.12.1, there are two methods by which we can write the Java thread
programs one is by extending thread class and the other is by implementing the
Runnable interface.
• The
run() method is the most important method in any threading program. By using
this method the thread's behaviour can be implemented. The run method can be
written as follows -
public
void run()
{
//statements
for implementing thread
}
For
invoking the thread's run method the object of a thread is required. This
object can be obtained by creating and initiating a thread using the start()
method.
• The
Thread class can be used to create a thread.
• Using
the extend keyword your class extends the Thread class for creation of thread.
For example if I have a class named A then it can be written as
class A
extends Thread
• Constructor
of Thread Class: Following are various syntaxes used for writing the
constructor of Thread Class.
Thread()
Thread(String
s)
Thread
(Runnable obj)
Thread(Runnable
obj, String s);
• Various
commonly used methods during thread programming are as given below -
Following
program shows how to create a single thread by extending Thread Class,
Java Program
class
MyThread extends Thread doin
{
public
void run()
{
System.out.println("Thread
is created!!!");
}
}
class
ThreadProg
{
public
static void main(String args[])
{
MyThread
t=new MyThread();
t.start();
}
}
Output
Program
Explanation :
In above
program, we have created two classes. One class named MyThread extends the
Thread class. In this class the run method is defined. This run method is
called by t.start() in main() method of class ThreadProg.
The
thread gets created and executes by displaying the message Thread is created.
Ex. 3.12.1: Create a thread by
extending the Thread Class. Also make use of constructor and display message
"You are Welcome to Thread Programming".
Sol.:
class
MyThread extends Thread
{
String
str=""; //data member of class MyThread
MyThread(String
s)//constructor
{
this.str=s;
}
public
void run()
{
System.out.println(str);
}
}
class
ThreadProg
{
public
static void main(String args[])
{
MyThread
t=new MyThread("You are Welcome to Thread Programming"); t.start();
}
}
Output
You are
Welcome to Thread Programming
• The
thread can also be created using Runnable interface.
• Implementing
thread program using Runnable interface is preferable than implementing
it by
extending the thread class because of the following two reasons -
1. If a
class extends a thread class then it can not extends any other class which may
be required to extend.
2. If a
class thread is extended then all its functionalities get inherited. This is an
expensive operation.se
• Following
Java program shows how to implement Runnable interface for creating a single
thread.
Java Program
class
MyThread implements Runnable
{
public
void run()
{
System.out.println("Thread
is created!");
}
}
class
ThreadProg Runn
{
public
static void main(String args[])
{
MyThread
obj = new MyThread();
Thread
t=new Thread(obj);
t.start();
}
}
Output
Program
Explanation :
• In
above program, we have used interface Runnable.
• While
using the interface, it is necessary to use implements keyword.
• Inside
the main method
1.
Create the instance of class MyClass.
2. This
instance is passed as a parameter to Thread class.
3. Using
the instance of class Thread invoke the start method.
4. The
start method in-turn calls the run method written in MyClass.
• The
run method executes and display the message for thread creation.
Ex. 3.12.2 Create a thread by
extending the Thread Class. Also make use of constructor and display message
"You are Welcome to Thread Programming".
Sol. :
class
MyThread implements Runnable
{
String
str;
MyThread(String
s)
{
this.str=s;
}
public
void run()
{
System.out.println(str);
}
}
class
ThreadProgRunn
{
public
static void main(String args[])
{
MyThread
obj = new MyThread("You are Welcome to Thread Programming");
Thread
t=new Thread(obj);
t.start();
}
}
Output
You are
Welcome to Thread Programming
Ex. 3.12.3: Write a Java program
that prints the numbers from 1 to 10 line by line after every 10 seconds.
Sol. :
Java Program[MultiThNum.java]
class
NumPrint extends Thread
{
int num;
NumPrint()
{
start();//directs
to the run method
}
public void
run()//thread execution starts
{
try
{
for(int
i=1;i<=10;i++)
{
System.out.println(i);
Thread.sleep(10000);//10
sec,b'coz 1000millisec=1 sec.
}
}
catch
(InterruptedException e)
{
System.out.println("Exception
in Thread handling");
}
}
}
public
class MultiThNum
{
public
static void main(String args[])
{
NumPrint
t1;
t1=new
NumPrint();
}
}
Output
C:>javac
MultiThNum.java
C:>java
MultiThNum
1
2
3
4
5
6
7
7
8
9
10
Review
Questions
1.
Explain how threads are created in Java.
2.How to
extends the thread class? Give an example.
3.How to
implement runnable interface for creating and starting threads?
4.What
is meant by concurrent programming? Define thread. Discuss the two ways of
implementing thread using example.
Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : with Example Java Programs - Creating a Thread
Object Oriented Programming
CS3391 3rd Semester CSE Dept | 2021 Regulation | 3rd Semester CSE Dept 2021 Regulation