Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

February 11, 2016 Categories: Multithreading. No Comments on Thread Methods

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:

 

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.

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:

 

Output of the above program is:

Child thread running!
Child thread running!
Child thread running!
Child thread running!
Child thread running!
Child thread stopped!

 

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up