In this article we will learn some of the thread methods like isAlive, join, suspend, resume, and stop along with sample Java programs.
isAlive() and join() Methods
Even though threads are independent most of the time, there might some instances at which we want a certain thread to wait until all other threads complete their execution. In such situations we can use isAlive() and join() methods. Syntax of these methods is as follows:
final boolean isAlive()
final void join() throws InterruptedException
final void join(long milliseconds) throws InterruptedException
The isAlive() method can be used to find whether the thread is still running or not. If the thread is still running, it will return true. Otherwise, it will return false.
The join() method makes the thread to wait until the invoking thread terminates. Below program demonstrates the use of isAlive() and join() methods:
class MyThread implements Runnable
{
Thread t;
MyThread()
{
t = new Thread(this);
System.out.println("Child thread: " + t);
t.start();
}
@Override
public void run()
{
try
{
for(int i = 1; i <= 3; i++)
{
System.out.println(t.getName() + ": " + i);
Thread.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Child thread is interrupted!");
}
System.out.println("Child thread is terminated");
}
}
public class Driver
{
public static void main(String[] args)
{
MyThread my = new MyThread();
try
{
System.out.println("Child thread is running: " + my.t.isAlive());
System.out.println("Main thread is waiting...");
my.t.join();
}
catch(InterruptedException e)
{
System.out.println("Main thread is interrupted!");
}
System.out.println("Child thread is running: " + my.t.isAlive());
System.out.println("Main thread terminated");
}
}
Output of the above program is:
Child thread: Thread[Thread-0,5,main]
Child thread is running: true
Main thread is waiting…
Thread-0: 1
Thread-0: 2
Thread-0: 3
Child thread is terminated
Child thread is running: false
Main thread terminated
In the above program, main thread is waiting for the child thread to complete its execution and then it terminates.
Suspending, Resuming and Stopping Threads
Based on the requirements sometimes you might want to suspend, resume or stop a thread. For doing such operations on a thread, before Java 2, thread API used to contain suspend(), resume() and stop() methods.
But all these methods might lead to undesired behavior of the program or machine. For example, these methods might lead to deadlock when a thread locked and is accessing a data structure and suddenly it is suspended or stopped. All other threads will be waiting indefinitely for gaining access to the data structure.
Because of this drawback of suspend(), resume() and stop() methods, they have been deprecated (should not be used) from Java 2 on wards.
Instead we have to check whether the thread is suspended or not through code itself. We can do that using a boolean variable which acts like a flag (ON / OFF).
Below program demonstrates the use of the boolean variable suspendFlag which is initially false. We write two more methods mySuspend() and myResume() which changes the value of the variable suspendFlag.
We have also used two more methods wait() and notify() from Object class. The wait() method suspends the current thread and the notify() method resumes (wakes up) the execution of the current thread.
class MyThread implements Runnable
{
Thread t;
boolean suspendFlag;
MyThread(String name)
{
t = new Thread(this, name);
System.out.println("Child thread: " + t);
suspendFlag = false;
t.start();
}
@Override
public void run()
{
try
{
for(int i = 1; i <= 5; i++)
{
System.out.println(t.getName() + ": " + i);
Thread.sleep(500);
synchronized(this)
{
while(suspendFlag)
wait();
}
}
}
catch(InterruptedException e)
{
System.out.println(t.getName() + " is interrupted!");
}
System.out.println(t.getName() + " is terminated");
}
public synchronized void mySuspend()
{
suspendFlag = true;
}
public synchronized void myResume()
{
suspendFlag = false;
notify();
}
}
public class Driver
{
public static void main(String[] args)
{
MyThread thread1 = new MyThread("First Thread");
MyThread thread2 = new MyThread("Second Thread");
try
{
System.out.println("Main thread is waiting...");
Thread.sleep(1000);
thread1.mySuspend();
System.out.println("---Suspending thread 1---");
Thread.sleep(1000);
thread1.myResume();
System.out.println("---Resuming thread 1---");
Thread.sleep(1000);
thread1.t.join();
thread2.t.join();
}
catch(InterruptedException e)
{
System.out.println("Main thread is interrupted!");
}
System.out.println("Main thread terminated");
}
}
Output of the above program is:
Child thread: Thread[First Thread,5,main]
Child thread: Thread[Second Thread,5,main]
Main thread is waiting…
First Thread: 1
Second Thread: 1
First Thread: 2
Second Thread: 2
—Suspending thread 1—
First Thread: 3
Second Thread: 3
Second Thread: 4
—Resuming thread 1—
First Thread: 4
Second Thread: 5
First Thread: 5
Second Thread is terminated
First Thread is terminated
Main thread terminated
In the above output you can observer that first thread is suspended for some time and has been resumed again.
Below program demonstrates stopping a thread:
class MyThread implements Runnable
{
Thread t;
private volatile boolean stopFlag = false;
MyThread()
{
t = new Thread(this);
t.start();
}
public void run()
{
while(!stopFlag)
{
System.out.println("Child thread running!");
try
{
Thread.sleep(200);
}
catch(InterruptedException e) {}
}
System.out.print("Child thread stopped!");
}
public void stop()
{
stopFlag = true;
}
}
public class Driver
{
public static void main(String[] args) throws InterruptedException
{
MyThread m = new MyThread();
Thread.sleep(1000);
m.stop();
}
}
Output of the above program is:
Child thread running!
Child thread running!
Child thread running!
Child thread running!
Child thread running!
Child thread stopped!
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply