Java Async Programming: Core Concepts and Best Practices

Asynchronous Programming in Java Java Level Async (Core Concepts) ✅ Runnable vs Callable At the very basic level, when you create a task: Runnable → Just runs something, doesn’t return anything Callable → Runs something and gives you a result back In real projects: Use Runnable for things like logging, background audit, notifications Use Callable when you’re calling a DB or another API and need a response Example: Runnable r = () -> System.out.println("Logging task"); Callable<String> c = () -> { return "DB Data"; }; ✅ Executor This is a very simple interface — just executes a task. In reality, you won’t use this directly much. It’s more like a base concept behind everything else. Example: Executor ex = Runnable::run; ex.execute(() -> System.out.println("Task executed")); ✅ ExecutorService (Very Important) This is where real-world usage starts. Instead of creating threads manually (which is costly), we use a thread pool. Why? Thread creation is expensive Reusing threads improves performance You get control over how many tasks run in parallel Typical scenarios: Processing thousands of records Calling multiple APIs in parallel Running batch jobs Example: ExecutorService ex = Executors.newFixedThreadPool(3); ex.submit(() -> { System.out.println(Thread.currentThread().getName()); }); ✅ Executors (Factory Class) 👉 Utility class to create thread pools Types: Fixed → Controlled threads Cached → Dynamic threads Single → Sequential execution Executors.newFixedThreadPool(5); Executors.newCachedThreadPool(); ->Quick setup (POC / small apps) ->Avoid direct use in production → use custom thread pool ✅ Future (Old Approach) ->Represents async result ->get() blocks the thread Future<String> f = ex.submit(() -> "Hello"); String res = f.get(); // blocks ->Blocking → reduces performance ->Legacy systems only ✅ CompletableFuture ⭐⭐⭐ (MOST IMPORTANT) ->Modern async API (Java 8+) Supports: Non-blocking execution Chaining Combining multiple tasks Exception handling CompletableFuture.supplyAsync(() -> "User") .thenApply(name -> name + " Data") .thenAccept(System.out::println); ->Parallel Calls Example CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> "Orders"); CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> "Payments"); CompletableFuture.allOf(f1, f2).join(); =>Real Scenarios: Aggregating microservice responses Calling multiple APIs in parallel Building dashboards =>Why it's powerful? Non-blocking → better performance Functional style → clean code ✅ ForkJoinPool -> Uses divide & conquer approach ForkJoinPool pool = new ForkJoinPool(); ->When to use? Large computations Recursive parallel processing ->Example: File parsing Data splitting tasks Spring level async techniques are upcoming post. #java #springboot #javadevelopement #spring #springboot #programming #coding

To view or add a comment, sign in

Explore content categories