Optimizing Code in Java
Pavlos Kosmetatos
Lead Engineer @Wealthyhood
Thread creation is expensive!
$$
Thread pools:
ExecutorService
interface// 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();
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();
// 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();
CompletableFuture
Function
// 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);
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