• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Creating a new thread for every task is expensive and unscalable — thread creation has overhead, too many threads cause contention and memory pressure, and managing thread lifecycles manually is error-prone. A better abstraction is needed for task execution.

Core Idea

The Executor framework decouples task submission from task execution. The core interfaces are Executor (single task execution), ExecutorService (lifecycle management), and ScheduledExecutorService (delayed/periodic tasks). Common implementations include ThreadPoolExecutor, Executors.newFixedThreadPool(), and Executors.newCachedThreadPool().

How It Works

Tasks (Runnable or Callable) are submitted to an ExecutorService. The framework maintains a pool of worker threads and a task queue. When a task is submitted, it’s either assigned to an available thread or queued. Callable tasks return a Future that can be queried for the result once computation completes.

Visual Explanation

java_executor Tasks Submitted Tasks T1 T2 T3 T4 T5 T6 Queue Blocking Queue (holds pending tasks) Tasks->Queue submit() Pool Thread Pool (worker threads) Queue->Pool W1 Worker 1 (executes T1) Pool->W1 W2 Worker 2 (executes T2) Pool->W2 W3 Worker 3 (executes T3) Pool->W3 Result1 Future<T1> W1->Result1 Result2 Future<T2> W2->Result2

Semantic Network

semantic_executor THIS Executor Framework THREAD Multithreading THIS--THREAD built from SYNC Synchronization THIS--SYNC built from LAMB Parallel Streams THIS--LAMB related PERF Performance THIS--PERF related

Key Properties

  • Thread reuse: Worker threads are recycled, avoiding creation overhead
  • Bounded queues: Prevents unbounded memory growth from pending tasks
  • Rejection policy: What happens when the queue is full (abort, discard, caller-runs)
  • Lifecycle control: shutdown() (no new tasks) and shutdownNow() (force stop)

Connections

Edge Cases & Gotchas

  • Hidden thread leak: Not shutting down an executor prevents JVM exit
  • Task submission inside tasks: Tasks submitted from within running tasks can cause thread pool deadlock
  • CachedThreadPool unbounded: newCachedThreadPool() creates threads without bound under load
  • ForkJoinPool work stealing: Each worker has its own deque — steals from others when idle