Java Thread States and Concurrency Fundamentals

Concurrency Programming Essentials

Thread Lifecycle States

  • NEW: Initial state after thread creation but before start() invocation
  • RUNNABLE: Combined ready/running state in JVM terminology
  • BLOCKED: Occurs when a thread fails to acquire a synchronized lock
  • WAITING: Indefinite pause triggered by wait(), join(), or I/O operations
  • TIMED_WAITING: Temporary pause with specified duration
  • TERMINATED: Final state after thread execution completes

Thread Monitoring Techniques

Use JDK diagnostic tools:

jps # Lists Java process IDs
jstack <pid> # Outputs thread stack traces

Thread Termination Methods

  • interrupt(): Sets interruption flag and unblocks waiting threads
  • interrupted(): Checks and clears interruption status
  • isInterrupted(): Checks without clearing status

Interruption Handling Example

class InterruptHandler implements Runnable {
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                TimeUnit.SECONDS.sleep(5);
                System.out.println("Operation executing");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                System.err.println("Interruption processed");
            }
        }
        System.out.println("Thread terminating");
    }

    public static void main(String[] args) throws Exception {
        Thread worker = new Thread(new InterruptHandler());
        worker.start();
        TimeUnit.SECONDS.sleep(2);
        worker.interrupt();
    }
}

Volatile Flag Implementation

public class TerminationController {
    private volatile static boolean shutdownSignal;

    public static void main(String[] args) throws Exception {
        Thread processor = new Thread(() -> {
            long counter = 0;
            while (!shutdownSignal) counter++;
        });
        
        processor.start();
        TimeUnit.SECONDS.sleep(1);
        shutdownSignal = true;
    }
}

Concurrency Challenges

CPU Cache Architecture

  • L1 Cache: 64KB total (32KB data/32KB instructions), ~1ns access
  • L2 Cache: 256KB per core, ~3ns access
  • L3 Cache: Shared multi-megabyte cache, ~12ns access

Cache Coherence Protocols

MESI states maintain consistency:

  • Modified (M): Dirty cache line
  • Exclusive (E): Clean cache line
  • Shared (S): Multiple readable copies
  • Invalid (I): Unusable cache data

Instruction Reordering

Compiler/processor optimizations may execute instructions non-sequentially

Java Memory Model (JMM)

Abstracts hardware-specific memory behaviors:

  • Main Memory: Central shared storage
  • Working Memory: Thread-local variable copies
  • Memory Barriers: Enforce visibility constraints

Guarantees atomicity, visibility, and ordering for concurrent operations.

Tags: Java Thread States Concurrency Control CPU Cache Coherence Memory Barriers Interrupt Handling

Posted on Thu, 14 May 2026 16:43:04 +0000 by techite