Java Multithreading Fundamentals and Implementation Approaches

Modern operating systems enable concurrent execution of multiple tasks through process management. A process represents an executing program with its own memory space, and the system allocates CPU time slices to each process, allowing them to execute in rotation.

Within each process, threads serve as independent execution paths. A single process can contain multiple concurrent threads, each receiving a portion of execution time, enabling parallel processing within the same application.

Thread Creation Methods

Java provides two primary approaches for creating threads: extending the Thread class and implementing the Runnable interface.

Extending Thread Class

The Thread class represents individual threads of execution. New threads are created by instantiating Thread objects.

Common Thread constructors include:

public Thread(String threadName); // Creates thread with specified name
public Thread();                   // Creates thread with default settings

Example implementation using class inheritance:

public class CounterThread extends Thread {
    private int limit = 10;
    
    @Override
    public void run() {
        while (limit > 0) {
            System.out.print(limit + " ");
            limit--;
            if (limit == 0) break;
        }
    }
    
    public static void main(String[] args) {
        new CounterThread().start();
    }
}

Implementing Runnable Interface

When a class needs to inherit from another superclass while also requiring multithreading capabilities, the Runnable interface provides the necessary functionality. This approach avoids the limitations of Java's single inheritance model.

For example, GUI applications that extend JFrame cannot simultaneously extend Thread. In such cases, implementing Runnable becomes essential.

Example combining Swing components with threading:

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

public class AnimatedFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    private JLabel displayLabel = new JLabel();
    private static Thread animationThread;
    private int position = 0;
    private Container contentPane = getContentPane();
    
    public AnimatedFrame() {
        setBounds(300, 200, 250, 100);
        contentPane.setLayout(null);
        
        URL resource = AnimatedFrame.class.getResource("/animation.gif");
        Icon graphic = new ImageIcon(resource);
        displayLabel.setIcon(graphic);
        displayLabel.setHorizontalAlignment(SwingConstants.LEFT);
        displayLabel.setBounds(10, 10, 200, 50);
        displayLabel.setOpaque(true);
        
        animationThread = new Thread(() -> {
            while (position <= 200) {
                displayLabel.setBounds(position, 10, 200, 50);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                position += 4;
                if (position >= 200) {
                    position = 10;
                }
            }
        });
        
        animationThread.start();
        contentPane.add(displayLabel);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args) {
        new AnimatedFrame();
    }
}

Tags: java multithreading thread Runnable Concurrency

Posted on Thu, 07 May 2026 20:08:17 +0000 by mogster