top of page
Writer's picturecompnomics

Multiple threads, resuming and stopping threads using the Thread class: Java


Here's a Java program demonstrating creating multiple threads, resuming and stopping threads using the Thread class:

Java

public class ThreadDemo {

  public static void main(String[] args) {

    // Create three threads
    MyThread thread1 = new MyThread("Thread 1");
    MyThread thread2 = new MyThread("Thread 2");
    MyThread thread3 = new MyThread("Thread 3");

    // Start the threads
    thread1.start();
    thread2.start();

    // Suspend thread2 for demonstration (can be removed for normal execution)
    try {
      thread2.suspend();
      System.out.println("Suspending Thread 2...");
      Thread.sleep(2000); // Wait for 2 seconds
    } catch (InterruptedException e) {
      System.out.println("Error suspending thread: " + e.getMessage());
    }

    // Resume thread2
    thread2.resume();
    System.out.println("Resuming Thread 2...");

    // Start thread3 after a delay
    try {
      Thread.sleep(1000); // Wait for 1 second
      thread3.start();
    } catch (InterruptedException e) {
      System.out.println("Error starting thread: " + e.getMessage());
    }

    // Wait for all threads to finish (optional)
    try {
      thread1.join();
      thread2.join();
      thread3.join();
      System.out.println("All threads finished!");
    } catch (InterruptedException e) {
      System.out.println("Error joining thread: " + e.getMessage());
    }
  }
}

class MyThread extends Thread {

  private String name;

  public MyThread(String name) {
    this.name = name;
  }

  @Override
  public void run() {
    for (int i = 0; i < 5; i++) {
      System.out.println(name + " running iteration: " + i);
      try {
        Thread.sleep(500); // Simulate some work (half-second delay)
      } catch (InterruptedException e) {
        System.out.println(name + " interrupted!");
        break;
      }
    }
  }
}

Explanation:

  1. ThreadDemo class:

  • The main method creates three MyThread objects with different names.

  • It starts thread1 and thread2 immediately using start().

  • It demonstrates suspending thread2 for 2 seconds using suspend() (Note: Suspend and resume are deprecated methods, use alternatives with caution).

  • It resumes thread2 using resume().

  • It starts thread3 after a 1-second delay using start().

  • It optionally waits for all threads to finish using join().

  1. MyThread class:

  • Extends the Thread class.

  • Has a constructor to set the thread name.

  • Overrides the run() method to define the thread's work.

  • The run() method iterates 5 times, printing a message with the thread name and iteration number.

  • It simulates work with a 0.5-second sleep using Thread.sleep().

  • It catches InterruptedException which might occur if the thread is interrupted (e.g., during suspend/resume).

Running the program:

This program will output something similar to:

Thread 1 running iteration: 0
Thread 1 running iteration: 1
Thread 1 running iteration: 2
Suspending Thread 2...
Thread 1 running iteration: 3
Thread 1 running iteration: 4
Resuming Thread 2...
Thread 2 running iteration: 0
Thread 2 running iteration: 1
Thread 3 running iteration: 0
Thread 1 finished!
Thread 2 running iteration: 2
Thread 2 finished!
Thread 3 running iteration: 1
Thread 3 running iteration: 2
Thread 3 running iteration: 3
Thread 3 running iteration: 4
Thread 3 finished!
All threads finished!

Important Notes:

  • Suspend and resume methods are deprecated in Java due to potential concurrency issues. It's recommended to use alternatives like thread pools and synchronization mechanisms for better control.

  • The join() method waits for the specified thread to finish before continuing. This can

32 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page