0% found this document useful (0 votes)
32 views5 pages

IT 2402notes-39

There are two main ways to create new threads in Java: 1) By extending the Thread class and overriding the run method or 2) By implementing the Runnable interface and overriding the run method. The Thread class contains useful methods like start(), sleep(), join(), isAlive(), etc. To start a new thread, the start() method must be called which in turn calls the run() method. Examples are provided that demonstrate creating new threads using both approaches.

Uploaded by

Vishal Kattamuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views5 pages

IT 2402notes-39

There are two main ways to create new threads in Java: 1) By extending the Thread class and overriding the run method or 2) By implementing the Runnable interface and overriding the run method. The Thread class contains useful methods like start(), sleep(), join(), isAlive(), etc. To start a new thread, the start() method must be called which in turn calls the run() method. Examples are provided that demonstrate creating new threads using both approaches.

Uploaded by

Vishal Kattamuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming through JAVA Unit – 3

Lecture Notes – 39
Creating Threads
Creating Threads:
To create a new thread a program will either:
1) extends the Thread class, or
2) implement the Runnable interface
Thread class encapsulates a thread of execution. The whole Java multithreading
environment is based on the Thread class.

Thread Methods:
• Start: a thread by calling start its run method
• Sleep: suspend a thread for a period of time
• Run: entry-point for a thread
• Join: wait for a thread to terminate
• isAlive: determine if a thread is still running
• getPriority: obtain a thread’s priority
• getName: obtain a thread’s name

New Thread: Runnable


To create a new thread by implementing the Runnable interface:
1) create a class that implements the run method (inside this method, we define the code
that constitutes the new thread):
public void run()
2) instantiate a Thread object within that class, a possible constructor is:
Thread(Runnable threadOb, String threadName)
3) call the start method on this object (start calls run):
void start()
Thread Example 1:
A class NewThread that implements Runnable:
class NewThread implements Runnable
{
Thread t;
//Creating and starting a new thread. Passing this to the

M. Satish (IT)
Object Oriented Programming through JAVA Unit – 3
// Thread constructor – the new thread will call this
// object’s run method:
NewThread()
{
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start();
}
//This is the entry point for the newly created thread – a five-iterations loop
//with a half-second pause between the iterations all within try/catch:
public void run()
{
try
{
for (int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ThreadDemo
{
public static void main(String args[])
{
//A new thread is created as an object of
// NewThread:
new NewThread();
//After calling the NewThread start method,
M. Satish (IT)
Object Oriented Programming through JAVA Unit – 3
// control returns here.
//Both threads (new and main) continue concurrently.
//Here is the loop for the main thread:
try
{
for (int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}

Creation of new Thread: Extend Thread


The second way to create a new thread: 1) create a new class that extends Thread and 2)
create an instance of that class
Thread provides both run and start methods:
1) the extending class must override run
2) it must also call the start method
• The new thread class extends Thread:
class NewThread extends Thread
{
//Create a new thread by calling the Thread’s
// constructor and start method:
NewThread()
{
super("Demo Thread");
System.out.println("Child thread: " + this);
start();
M. Satish (IT)
Object Oriented Programming through JAVA Unit – 3
}
NewThread overrides the Thread’s run method:
public void run()
{
try
{
for (int i = 5; i > 0; i--)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
class ExtendThread
{
public static void main(String args[])
{
//After a new thread is created:
new NewThread();
//the new and main threads continue
//concurrently…
//This is the loop of the main thread:
try
{
for (int i = 5; i > 0; i--)
{
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}
M. Satish (IT)
Object Oriented Programming through JAVA Unit – 3
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}
}

M. Satish (IT)

You might also like