Skip to main content

Java Multi-Threading

 Callable with Future 

Future can be used along with callable to get return from the threads execution.

public String getDataWithGET(String url, int i) {
        System.out.println("Stated" + i);
        WebClient web = WebClient.create(url);

        String x = web.get().exchangeToMono(
            t -> t.statusCode().equals(HttpStatusCode.valueOf(200)) ? t.bodyToMono(String.class) : t.createError())
            .block();
    System.out.println(x);
    return x;
}

void getData() {
    try {
        ExecutorService exe = Executors.newFixedThreadPool(10);
        ArrayList<Future<String>> listFuture = new ArrayList<Future<String>>();
        for (int i = 0; i < 20; i++) {
            int num = i;
            String url = "https://www.deckofcardsapi.com/api/deck/new/shuffle/?deck_count=" + i;
            Callable<String> c = () -> getDataWithGET(url, num);
            Future<String> future = exe.submit(c);
            listFuture.add(future);
        }
        System.out.println("Fetting done : Response size "+listFuture.size());
        for(Future<String> f: listFuture) {
            System.out.println(f.get());
        }
        exe.shutdown();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Ref : https://www.digitalocean.com/community/tutorials/java-callable-future-example



Comments

Popular posts from this blog

Spring Boot 4 Ready

Spring Boot 4.x Features Explained:  Java 25, AI Agents, MCP, Resilience, gRPC & Cloud Native Evolution Building on the massive architectural shift introduced in version 3, Spring Boot 4.x bridges the gap between traditional enterprise stability and the rapid demands of modern cloud architecture and AI integration. While Spring Boot 3 introduced the foundations for GraalVM Native Images and Virtual Threads , Spring Boot 4 takes these capabilities mainstream, introduces native resilience patterns, and upgrades the underlying Spring ecosystem. Spring Boot 4 Major Theme: Modern enterprise development with cloud-native architecture, AI integration, improved resilience, better modularity, and optimized JVM performance. Table of Contents 1. Upgraded Foundation & Baselines 2. Built-in Core Resilience Features 3. First-Class Agentic AI & Model Context Protocol (MCP) 4. Native API Versioning Strategy 5. Modern...

Most questions in 2025

Java Spring Boot Senior Engineer Interview Questions & Answers A complete interview preparation guide for Senior Java Engineers, Spring Boot Developers, and Backend Technical Leads covering Java, Spring Boot, Microservices, Cloud, Database, Security, and Performance Engineering concepts. Target Audience: Senior Java Developers Spring Boot Engineers Microservice Architects Backend Technical Leads Table of Contents 1. Describe Yourself 2. RESTful APIs and Standards 3. Latest Spring Boot Version & Features 4. Java Optional 5. WebFlux Reactive Programming 6. WebFlux Retry Mechanism 7. WebFlux Pagination APIs 8. Circuit Breaker 9. Resilience Patterns 10. Rate Limiter 1. Describe Yourself A senior engineer introduction should highlight technical experience, architecture knowledge, ownership, and problem-solving capabilities. Sample Answer I am a Java backend engineer with 7+ years of experience desi...

Data Structure

Java Data Structures A highly-scannable architectural cheat sheet for developers and technical interviews. 1. Linear vs. Non-Linear: The Core Division Data Structure Classification Hierarchy ┌───────────────────────┐ │ Data Structures │ └───────────┬───────────┘ │ ┌───────────────┴───────────────┐ ▼ ▼ ┌────────────────────┐ ┌────────────────────┐ │ Linear (Seq) │ │ Non-Linear │ └──────────┬─────────┘ └──────────┬─────────┘ │ │ ┌──────┴──────┐ ┌──────┴──────┐ ▼ ▼ ▼ ▼ [Static] [Dynamic] [Trees] [Graphs] (Array) (Queue, Stack) (BST) (Network) ...