Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
Windows Tipps & SecurityGrafikkarte vor Überhitzung schützen: So geht’s(25.09.2026 um 08:00 Uhr)
••••••••••
Windows Tipps & SecurityGrafikkarte vor Überhitzung schützen: So geht’s(25.09.2026 um 08:00 Uhr)
••••••••••
Intelligence View
⚡ tsecurity.de Intelligence

⚡ Advanced `CompletableFuture` Use Cases in Java

In the previous section, we covered the basics of CompletableFuture: chaining, combining, exception handling, and real-world use cases. Now, let’s go one step further and look at advanced features that make it production-ready: …

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

In the previous section, we covered the basics of CompletableFuture: chaining, combining, exception handling, and real-world use cases. Now, let’s go one step further and look at advanced features that make it production-ready:









8. Setting Timeouts (orTimeout & completeOnTimeout)



Sometimes tasks hang due to slow APIs or network issues. Instead of blocking forever, CompletableFuture lets you set timeouts.






orTimeout() → fail after timeout






import java.util.concurrent.*;

public class CompletableFutureTimeout {
public static void main(String[] args) {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try { Thread.sleep(3000); } catch (InterruptedException e) {}
return "Completed!";
}).orTimeout(2, TimeUnit.SECONDS);

try {
System.out.println(future.get());
} catch (Exception e) {
System.out.println("Timeout! " + e.getMessage());
}
}
}






👉 After 2 seconds, the future throws a TimeoutException.






completeOnTimeout() → return default value if timeout






public class CompletableFutureCompleteOnTimeout {
public static void main(String[] args) throws Exception {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try { Thread.sleep(3000); } catch (InterruptedException e) {}
return "Data loaded!";
}).completeOnTimeout("Fallback value", 2, TimeUnit.SECONDS);

System.out.println(future.get()); // prints "Fallback value"
}
}












9. Cancelling a Task



If a task is no longer needed, you can cancel it.




import java.util.concurrent.*;

public class CompletableFutureCancel {
public static void main(String[] args) throws Exception {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(5000);
return "Finished!";
} catch (InterruptedException e) {
return "Interrupted!";
}
});

Thread.sleep(1000);
boolean cancelled = future.cancel(true); // tries to stop execution

System.out.println("Cancelled: " + cancelled);
}
}












10. Using Custom ExecutorService



By default, CompletableFuture uses the ForkJoinPool.commonPool(). For better control, especially in production, you should provide a custom thread pool.




import java.util.concurrent.*;

public class CompletableFutureCustomExecutor {
public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(4);

CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
return "Running in custom executor: " + Thread.currentThread().getName();
}, executor);

System.out.println(future.get());
executor.shutdown();
}
}






📌 Why use a custom executor?




  • Prevents blocking the common pool with long tasks.

  • Lets you tune thread counts for CPU-bound vs I/O-bound workloads.









11. Any-of Pattern (anyOf)



When you want the fastest response from multiple tasks.




import java.util.concurrent.*;

public class CompletableFutureAnyOf {
public static void main(String[] args) throws Exception {
CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
sleep(2000);
return "Result from service 1";
});

CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
sleep(1000);
return "Result from service 2";
});

CompletableFuture<Object> fastest = CompletableFuture.anyOf(f1, f2);

System.out.println("Winner: " + fastest.get());
}

private static void sleep(int ms) {
try { Thread.sleep(ms); } catch (InterruptedException e) {}
}
}






👉 Output:




Winner: Result from service 2












12. Pipelining with Dependent Async Calls



Imagine fetching user details, then fetching their orders.




public class CompletableFuturePipeline {
public static void main(String[] args) throws Exception {
CompletableFuture<String> userFuture = CompletableFuture.supplyAsync(() -> {
sleep(1000);
return "User123";
});

CompletableFuture<String> ordersFuture = userFuture.thenCompose(user ->
CompletableFuture.supplyAsync(() -> {
sleep(1500);
return "Orders for " + user;
})
);

System.out.println(ordersFuture.get());
}

private static void sleep(int ms) {
try { Thread.sleep(ms); } catch (InterruptedException e) {}
}
}






👉 thenCompose() is used when one async task depends on another.









🔑 Key Takeaways





  • Timeouts prevent tasks from blocking forever.


  • Cancellation helps stop unnecessary tasks.


  • Custom executors give better performance control.


  • anyOf/allOf enable powerful parallel task handling.


  • thenCompose is the go-to for dependent async calls.









✅ Final Thoughts



CompletableFuture is not just about async execution—it’s a framework for orchestrating tasks:




  • Run tasks in parallel

  • Chain dependent pipelines

  • Handle timeouts, retries, errors

  • Customize execution with executors



When used correctly, it helps build high-performance, non-blocking, resilient applications in Java.






Would you like me to also include a full mini-project example (like fetching from 3 services with timeout, fallback, and parallel execution) so you can see how all these features work together in real-world code?

1. Sofort-Triage & Abwehrmaßnahmen

SOC Incident Playbook: Vulnerability Remediation & Verification
Syntax validiert (0 Fehler)
title: Detect Exploitation - ⚡ Advanced `CompletableFuture` Use Cases in Java
id: 672574d3-3283-4c69-8a87-2d439ad2da83
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-25
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
Syntax validiert (0 Fehler)
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-25"
        description = "YARA Signature for "
    strings:
        $str = "⚡ Advanced `CompletableFuture`" ascii wide
    condition:
        any of them
}
Syntax validiert (0 Fehler)
index=security sourcetype IN ("cisco:asa", "pan:traffic", "zeek_conn", "suricata", "WinEventLog:Security")
("Advanced CompletableFuture Use Cases in ")
| stats count earliest(_time) as first_seen latest(_time) as last_seen by src_ip, dest_ip, dest_host, signature
| eval first_seen=strftime(first_seen, "%Y-%m-%d %H:%M:%S"), last_seen=strftime(last_seen, "%Y-%m-%d %H:%M:%S")
| sort - count
Syntax validiert (0 Fehler)
message: "*Advanced CompletableFuture Use Cases in *"
Syntax validiert (0 Fehler)
CommonSecurityLog
| where Message has "Advanced CompletableFuture Use Cases in "
| summarize EventCount = count(), FirstSeen = min(TimeGenerated), LastSeen = max(TimeGenerated) by SourceIP, DestinationIP, DestinationPort, Activity
| extend DetectionRule = "iShareStuff-CTI-Compiled"
| sort by EventCount desc

2. Cyber Threat Intelligence & Forensik

🎯
MITRE ATT&CK Matrix Navigator 14 Taktiken
Reconnaissance
-
Resource Development
-
Initial Access
Execution
Persistence
-
Privilege Escalation
Defense Evasion
Credential Access
-
Discovery
-
Lateral Movement
-
Collection
-
Command and Control
Exfiltration
-
Impact
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich ⚡ Advanced `CompletableFuture` Use Cases.... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

⚡ Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten ⚡ Advanced `CompletableFuture` Use Cases in Java

Thematisch verwandte Begriffe: Advanced, CompletableFuture, Cases, Java · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93647 | An unauthenticated calendar sender can place active markup in a COUNTER …
Advisory →
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag