Advanced threading patterns

Optimizing Code in Java

Pavlos Kosmetatos

Lead Engineer @Wealthyhood

Thread pool fundamentals

Thread creation is expensive!

$$

Thread pools:

  • Reuse existing threads
  • Control number of concurrent threads
  • Are managed through ExecutorService interface
Optimizing Code in Java

Creating thread pools

// Fixed thread pool with 4 threads
ExecutorService fixedPool = Executors.newFixedThreadPool(4);


// Cached thread pool that grows as needed ExecutorService cachedPool = Executors.newCachedThreadPool();
// Single-threaded executor ExecutorService singleExecutor = Executors.newSingleThreadExecutor();
Optimizing Code in Java

Submitting tasks

ExecutorService executor = Executors.newFixedThreadPool(4);

// Submit a task with no return value
executor.execute(() -> System.out.println("Simple task"));

// Submit a task with a return value (Callable)
Future<Integer> future = executor.submit(() -> {
    Thread.sleep(1000);
    return 42;
});

// Get result from Future (blocks until complete)
int result = future.get();
Optimizing Code in Java

Shutting down executors

// Signal shutdown, but continue running existing tasks
executor.shutdown();


// Wait for termination (with timeout) boolean terminated = executor.awaitTermination(5, TimeUnit.SECONDS);
// Force immediate shutdown, canceling running tasks executor.shutdownNow();
Optimizing Code in Java

The completable future

  • CompletableFuture
  • Part of Java's concurrency API since Java 8
  • Modern approach to asynchronous programming
  • Can be completed manually or via a Function
  • Allows chaining operations with callbacks
  • Works with or without explicit thread pools
Optimizing Code in Java

Creating completable futures

// Run async with default executor
CompletableFuture<Void> runAsync = 
    CompletableFuture.runAsync(() -> performTask());

// Supply async with custom executor ExecutorService executor = Executors.newCachedThreadPool(); CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(() -> fetchData(), executor);
Optimizing Code in Java

Chaining operations

CompletableFuture<String> future = CompletableFuture
    .supplyAsync(() -> fetchUserData(userId))
    .thenApply(data -> extractUsername(data))
    .exceptionally(ex -> "Unknown user");

// Access to result when ready
future.thenAccept(result -> System.out.println(result));
Optimizing Code in Java

Let's practice!

Optimizing Code in Java

Preparing Video For Download...