1. The “Why” In a standard web application, a “task” is usually a single HTTP request.
The Goal: Isolate users from each other. If User A’s request takes 10 seconds to process a heavy database query, User B should still get their response in 100ms on a different thread. The Implementation: The server maintains a “Thread Pool.” When a request arrives, the “Boss” thread grabs a “Worker” thread from the pool, hands it the socket, and says, “Call me when you’re done.” 2. Comparison: One Thread vs. Thread Pool (Thread-Per-Task) Feature Single Threaded Thread-Per-Task (Pool) Concurrency None (One at a time). High (N tasks at a time). Isolation One crash kills the server. One crash only kills that thread. Throughput Very Low. High (Parallel processing). Blocking One slow DB call stops everyone. Only that specific worker thread is blocked. 3. The “Golden” Snippet: Executor-Based Web Server Instead of creating a new Thread() every time (which is slow), we use a FixedThreadPool to reuse existing threads.
...