Skip to content

Commit e64bab6

Browse files
authored
Update java21-features.md
1 parent 6dd325d commit e64bab6

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

java21-features.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,3 +334,99 @@ Files.copy(Path.of("source.txt"), Path.of("destination.txt"),
334334

335335
---
336336

337+
## 🃏Executor Framework - Key Classes & Methods
338+
339+
**Rule:** Understand the relationship between Executor, ExecutorService, and Executors utility class.
340+
341+
**Class Hierarchy:**
342+
* `Executor` interface → executes `Runnable` only
343+
* `ExecutorService` interface → extends Executor, executes both `Runnable` and `Callable`
344+
* `Executors` utility class → provides static factory methods (all start with "new")
345+
346+
**Essential Executors Factory Methods:**
347+
348+
```java
349+
import java.util.concurrent.*;
350+
351+
// Fixed thread pool - reuses fixed number of threads
352+
ExecutorService fixed = Executors.newFixedThreadPool(4);
353+
354+
// Single thread executor - sequential execution
355+
ExecutorService single = Executors.newSingleThreadExecutor();
356+
357+
// Cached thread pool - creates threads as needed, reuses idle ones
358+
ExecutorService cached = Executors.newCachedThreadPool();
359+
360+
// Scheduled executors for delayed/periodic tasks
361+
ScheduledExecutorService singleScheduled = Executors.newSingleThreadScheduledExecutor();
362+
ScheduledExecutorService multiScheduled = Executors.newScheduledThreadPool(3);
363+
364+
// Virtual threads (Java 21) - lightweight threads
365+
ExecutorService virtual = Executors.newVirtualThreadPerTaskExecutor();
366+
```
367+
368+
**Practical Example:**
369+
370+
```java
371+
// Basic Executor - only Runnable
372+
Executor basicExecutor = Executors.newSingleThreadExecutor();
373+
basicExecutor.execute(() -> System.out.println("Running task"));
374+
375+
// ExecutorService - both Runnable and Callable
376+
ExecutorService service = Executors.newFixedThreadPool(2);
377+
378+
// Execute Runnable
379+
service.execute(() -> System.out.println("Runnable task"));
380+
381+
// Submit Callable
382+
Future<String> result = service.submit(() -> {
383+
Thread.sleep(1000);
384+
return "Callable result";
385+
});
386+
387+
try {
388+
System.out.println(result.get()); // "Callable result"
389+
} catch (Exception e) {
390+
e.printStackTrace();
391+
} finally {
392+
service.shutdown();
393+
}
394+
```
395+
396+
**Thread Pool Comparison:**
397+
398+
```java
399+
// newFixedThreadPool(4) - exactly 4 threads, queues excess tasks
400+
ExecutorService fixed = Executors.newFixedThreadPool(4);
401+
402+
// newCachedThreadPool() - creates threads as needed, kills idle ones after 60s
403+
ExecutorService cached = Executors.newCachedThreadPool();
404+
405+
// newSingleThreadExecutor() - exactly 1 thread, tasks execute sequentially
406+
ExecutorService single = Executors.newSingleThreadExecutor();
407+
408+
// newVirtualThreadPerTaskExecutor() - creates new virtual thread per task
409+
ExecutorService virtual = Executors.newVirtualThreadPerTaskExecutor();
410+
```
411+
412+
**Common Pitfalls:**
413+
414+
```java
415+
// Pitfall 1: Trying to submit Callable to basic Executor
416+
Executor executor = Executors.newSingleThreadExecutor();
417+
// executor.submit(() -> "result"); // ❌ Executor doesn't have submit()
418+
419+
// Pitfall 2: Forgetting shutdown
420+
ExecutorService service = Executors.newFixedThreadPool(2);
421+
service.submit(() -> "task");
422+
// Missing service.shutdown(); // ❌ JVM won't terminate
423+
424+
// Pitfall 3: Wrong method name
425+
// Executors.createFixedThreadPool(4); // ❌ No such method
426+
ExecutorService correct = Executors.newFixedThreadPool(4); //
427+
```
428+
429+
**💡 Learning Tip:** "NEW executors create, SUBMIT callables" - Executors factory methods start with 'new', ExecutorService can 'submit' Callables (not basic Executor).
430+
431+
**Q:** What's the difference between Executor and ExecutorService interfaces?
432+
**A:** Executor only executes Runnable with execute(), while ExecutorService extends Executor and can submit both Runnable and Callable, returning Future objects.

0 commit comments

Comments
 (0)