Understanding Thread Lifecycle and Control in Java

Threads in Java have a lifecycle consisting of seven states: New, Runnable, Running, Waiting, Sleeping, Blocked, and Dead.

  1. Thread Sleep Mechanism

Example: Creating a SleepMethodTest class that extends JFrame to draw colored lines with random colors.

import java.awt.*;
import java.util.*;
import javax.swing.*;

public class SleepMethodTest extends JFrame {
    private Thread drawingThread;
    private static Color[] colors = {Color.BLACK, Color.BLUE, Color.CYAN,
            Color.GREEN, Color.ORANGE, Color.YELLOW, Color.RED,
            Color.PINK, Color.LIGHT_GRAY};
    private static final Random rand = new Random();

    private static Color getRandomColor() {
        return colors[rand.nextInt(colors.length)];
    }

    public SleepMethodTest() {
        drawingThread = new Thread(() -> {
            int x = 30;
            int y = 50;
            while (true) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Graphics g = getGraphics();
                g.setColor(getRandomColor());
                g.drawLine(x, y, 100, y++);
                if (y >= 80) y = 50;
            }
        });
        drawingThread.start();
    }

    public static void main(String[] args) {
        initializeFrame(new SleepMethodTest(), 100, 100);
    }

    private static void initializeFrame(JFrame frame, int width, int height) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}
  1. Thread Joining

Example: Creatign a JoinTest class with two progress bars where one waits for the other to copmlete.

import java.awt.*;
import javax.swing.*;

public class JoinTest extends JFrame {
    private Thread primaryThread;
    private Thread secondaryThread;
    final JProgressBar mainProgress = new JProgressBar();
    final JProgressBar secondaryProgress = new JProgressBar();

    public JoinTest() {
        getContentPane().add(mainProgress, BorderLayout.NORTH);
        getContentPane().add(secondaryProgress, BorderLayout.SOUTH);
        mainProgress.setStringPainted(true);
        secondaryProgress.setStringPainted(true);

        primaryThread = new Thread(() -> {
            int count = 0;
            while (true) {
                mainProgress.setValue(++count);
                try {
                    Thread.sleep(100);
                    secondaryThread.join();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        secondaryThread = new Thread(() -> {
            int count = 0;
            while (count < 100) {
                secondaryProgress.setValue(++count);
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        primaryThread.start();
        secondaryThread.start();
    }

    public static void main(String[] args) {
        initializeFrame(new JoinTest(), 100, 100);
    }

    private static void initializeFrame(JFrame frame, int width, int height) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}
  1. Thread Interruption

Example: Creating an InterruptedSwing class to demonstrate thread interruption with a progress bar.

import java.awt.*;
import javax.swing.*;

public class InterruptedSwing extends JFrame {
    private Thread progressThread;

    public InterruptedSwing() {
        final JProgressBar progress = new JProgressBar();
        getContentPane().add(progress, BorderLayout.NORTH);
        progress.setStringPainted(true);

        progressThread = new Thread(() -> {
            int count = 0;
            while (true) {
                progress.setValue(++count);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("Thread was interrupted");
                    break;
                }
            }
        });

        progressThread.start();
        progressThread.interrupt();
    }

    public static void main(String[] args) {
        initializeFrame(new InterruptedSwing(), 100, 100);
    }

    private static void initializeFrame(JFrame frame, int width, int height) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}

Tags: java multithreading Thread Lifecycle Thread Control

Posted on Thu, 14 May 2026 02:00:02 +0000 by MiCR0