Threads
A thread is a lightweight unit of execution within a process. Multiple threads share the same process memory but execute independently.
Process vs Thread
Process (JVM)
+----------------------------------+
| Thread 1: Handle Request A |
| Thread 2: Handle Request B |
| Thread 3: Handle Request C |
| |
| Shared: Memory, Objects, DB Pool |
+----------------------------------+
Thread Lifecycle
NEW --> RUNNABLE --> RUNNING --> TERMINATED
^ |
| v
+---- BLOCKED/WAITING
| State | Description |
|---|---|
| NEW | Thread created but not started |
| RUNNABLE | Ready to run, waiting for CPU |
| RUNNING | Currently executing |
| BLOCKED | Waiting for a lock |
| WAITING | Waiting indefinitely |
| TIMED_WAITING | Waiting for a time period |
| TERMINATED | Finished execution |
Thread in Backend
Each incoming request is handled by a thread:
Request 1 --> Thread Pool --> Thread 1 processes request
Request 2 --> Thread Pool --> Thread 2 processes request
Request 3 --> Thread Pool --> Thread 3 processes request
Thread Pool
Instead of creating a new thread per request, use a thread pool:
Thread Pool (10 threads)
|
+--> Thread 1: Busy
+--> Thread 2: Available
+--> Thread 3: Busy
+--> ...
+--> Thread 10: Available
Benefits:
- Reuse threads instead of creating new ones
- Limit concurrent requests
- Prevent resource exhaustion
Java Threads
// Creating threads
Thread t = new Thread(() -> {
System.out.println("Running in thread: " + Thread.currentThread().getName());
});
t.start();
// Using ExecutorService
ExecutorService executor = Executors.newFixedThreadPool(10);
executor.submit(() -> processRequest());
Why Threads Matter for Backend
- Each request is handled by a thread
- Too few threads = requests wait
- Too many threads = resource exhaustion
- Thread pools balance throughput and resource usage
Thread Management
Thread Pools
ExecutorService pool = Executors.newFixedThreadPool(10);
Pool Types
- Fixed: Fixed number of threads
- Cached: Creates as needed
- Scheduled: For delayed tasks
- WorkStealing: ForkJoinPool
Best Practices
- Size pools appropriately
- Use meaningful thread names
- Implement graceful shutdown
- Monitor thread usage
Common Issues
- Thread leaks
- Context switching overhead
- Deadlocks
Key Points
- Understanding Threads is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Threads in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Threads implementation
// Key aspects: validation, error handling, logging, testing
public class Threads {
// Production-ready implementation
}Identify and handle edge cases for Threads. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Threads. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. What is a thread pool?
2. What happens when all threads in a pool are busy?
3. What is the primary purpose of Threads?
4. What is a common mistake when implementing Threads?
Flashcards
Question
What is a thread?
Click to reveal answer
Answer
A lightweight unit of execution within a process
Question
Why use thread pools?
Click to reveal answer
Answer
Reuse threads, limit concurrency, prevent resource exhaustion
Question
What is Threads?
Click to reveal answer
Answer
Threads is a key concept in backend development.
Question
When to use Threads?
Click to reveal answer
Answer
Use Threads when building production systems that require reliability, scalability, and maintainability.
Question
Threads best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Thread = lightweight execution unit within a process
- 2.Thread pool reuses threads for efficiency
- 3.Each request handled by one thread
- 4.Balance thread count for optimal performance
Interview Tips
- •Explain thread pools and their benefits
- •Know the thread lifecycle states
Cheat Sheet
Threads
- Thread: Lightweight unit within a process
- Thread Pool: Reusable set of threads
- States: NEW -> RUNNABLE -> RUNNING -> TERMINATED
- Backend: Each request = one thread from pool