Threads Summary
Thread Concept
- Unit of program execution
- Independent sequence of operations
- Can run concurrently
- Example:
Thread t = new Thread(() -> { System.out.println("Running in new thread"); }); t.start();
Creating Threads
-
Using Runnable:
Runnable task = () -> { // task code here }; Thread thread = new Thread(task); thread.start();
-
Extending Thread:
class MyThread extends Thread { public void run() { // task code here } } new MyThread().start();
Thread States
- NEW: Created but not yet started
- RUNNABLE: Executing or ready to execute
- BLOCKED: Waiting for monitor lock
- WAITING: Waiting indefinitely
- TIMED_WAITING: Waiting for specified time
- TERMINATED: Completed execution
Thread Operations
-
Basic Operations:
thread.start(); // start thread thread.join(); // wait for completion thread.sleep(1000); // pause execution thread.interrupt(); // interrupt thread
-
Thread Information:
Thread.currentThread().getName(); // get name Thread.currentThread().isAlive(); // check if alive Thread.currentThread().setPriority(5); // set priority
Thread Safety Issues
-
Race Conditions:
// Unsafe counter++; // not atomic // Safe synchronized(lock) { counter++; }
-
Visibility Problems:
// May have visibility issues boolean flag = false; // Guaranteed visibility volatile boolean flag = false;
Best Practices
- Use thread pools when possible
- Handle interruptions properly
- Avoid shared mutable state
- Use proper synchronization
- Document thread safety
- Test concurrent behavior
- Example: ```java try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // handle interruption }