Java Thread Control: Managing Thread Behavior in Multithreaded Applications

Discover how to control threads in Java, including suspending, resuming, and stopping threads in multithreaded applications. Learn about various static methods available to manage Java thread behavior effectively, ensuring optimal application performance.



Java Thread Control

Core Java offers comprehensive control over multithreaded programs. You can develop a multithreaded application that can be suspended, resumed, or completely stopped according to your needs. Various static methods can be applied to thread objects to manage their behavior effectively.

Methods for Controlling Java Threads

The following table outlines the methods available for controlling threads in Java:

Sr. No. Method & Description
1 public void suspend()
This method puts a thread into a suspended state, which can be resumed using the resume() method.
2 public void stop()
This method completely stops a thread.
3 public void resume()
This method resumes a thread that was previously suspended using the suspend() method.
4 public void wait()
Causes the current thread to wait until another thread invokes the notify() method.
5 public void notify()
Wakes up a single thread that is waiting on this object's monitor.

Note that the suspend(), resume(), and stop() methods have been deprecated in the latest versions of Java, so it is advisable to use alternative methods.

Example of Thread Control in Java

Syntax

class RunnableDemo implements Runnable {
public Thread t;
private String threadName;
boolean suspended = false;

RunnableDemo(String name) {
    threadName = name;
    System.out.println("Creating " + threadName);
}

public void run() {
    System.out.println("Running " + threadName);
    try {
        for (int i = 5; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            Thread.sleep(300);
            synchronized (this) {
                while (suspended) {
                    wait();
                }
            }
        }
    } catch (InterruptedException e) {
        System.out.println("Thread " + threadName + " interrupted.");
    }
    System.out.println("Thread " + threadName + " exiting.");
}

public void start() {
    System.out.println("Starting " + threadName);
    if (t == null) {
        t = new Thread(this, threadName);
        t.start();
    }
}

void suspend() {
    suspended = true;
}

synchronized void resume() {
    suspended = false;
    notify();
}
}

public class TestThread {
public static void main(String args[]) {
    RunnableDemo R1 = new RunnableDemo("Thread-1");
    R1.start();

    RunnableDemo R2 = new RunnableDemo("Thread-2");
    R2.start();

    try {
        Thread.sleep(1000);
        R1.suspend();
        System.out.println("Suspending First Thread");
        Thread.sleep(1000);
        R1.resume();
        System.out.println("Resuming First Thread");
        
        R2.suspend();
        System.out.println("Suspending Thread Two");
        Thread.sleep(1000);
        R2.resume();
        System.out.println("Resuming Thread Two");
    } catch (InterruptedException e) {
        System.out.println("Main thread Interrupted");
    }
    try {
        System.out.println("Waiting for threads to finish.");
        R1.t.join();
        R2.t.join();
    } catch (InterruptedException e) {
        System.out.println("Main thread Interrupted");
    }
    System.out.println("Main thread exiting.");
}
}
Output

Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 5
Running Thread-2
Thread: Thread-2, 5
Thread: Thread-1, 4
Thread: Thread-2, 4
Suspending First Thread
Thread: Thread-2, 3
Resuming First Thread
Suspending Thread Two
Thread: Thread-1, 3
Thread: Thread-1, 2
Resuming Thread Two
Waiting for threads to finish.
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Main thread exiting.