frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Understanding Java's Asynchronous Journey

https://amritpandey.io/understanding-javas-asynchronous-journey/
17•hardasspunk•11mo ago

Comments

Neywiny•11mo ago
I don't get it. The first example in JS vs Java looks very similar. Now all those other code blocks, they certainly have more going on but idk how that compares to JS. And to answer the questions:

A completable future is something that in the future may complete. I think that's self explanatory. A promise seems equally vague.

Boilerplate looks the same. JS is just a function, Java they put a class around it. Java requires exception handling which is annoying but having fought errors in async JS, I'll take all I can get.

API is eh. Sure. But that's not even shown in this example so I have no idea.

So JS saves like 3 lines? Is that really so much better?

cogman10•11mo ago
> A completable future is something that in the future may complete. I think that's self explanatory.

But not the reason for the name :).

It's called "completable" because these futures have a method on them `future.complete("value")`. Before their introduction, there was a `Future` API that java had.

nogridbag•11mo ago
Yeah that first example is rather poor. And it uses the word boilerpate to seemingly refer to the stuff unrelated to the async code (class declaration, exception handling, main method).

I don't use Java async much, but I guess if you have a utility method named "setTimeout" than the example can simply be:

    public CompletableFuture<String> fetchData() {
        return setTimeout(() -> "Data Fetched", 10000);
    }

    public void loadData() {
        fetchData().thenAccept(System.out::println);
    }
Which is simpler or equivalent to the JS example.
stevoski•11mo ago
The Java 1 example uses lambdas, which were introduced in Java 8.

It’s probably intentional, because it allows showing the Java 1 Thread approach succinctly.

But as long-term Java person, I find it jarring.

philipwhiuk•11mo ago
Java's had `var` since Java 10 but apparently the author deliberately ignored that to make the example as wordy as possible.

It's a little tiring to read a Java example with an entry-point (the public-static-void bit) and then a JavaScript example without one.

If you strip that out the original Java is:

  var future = CompletableFuture.supplyAsync(() -> {
        try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "Data Fetched";
        });
  future.thenAccept(result -> System.out.println(result));
  System.out.println("Prints first"); // prints before the async result
which is only obtuse due to checked exceptions.

Arguably it's still a different thing you're doing, because it's not scheduling a task on a pool, it's creating a thread which sleeps for 10 seconds.

elric•11mo ago
`var` is very unhelpful in situations where the reader might not be entirely familiar with the context, especially when using factory methods.

I don't think the author was trying to make the example "wordy" so much as "clear".

cogman10•11mo ago
Also, arguably, the wrong way to do something like this.

The author uses `setTimeout` for javascript. The equivalent for Java is either the `Timer` class or a `ScheduledExecutorService`. Doing a `Thread.sleep` simply isn't how you should approach this.

With that in mind, if you want to use both these things and keep the completable future interface you'd have to do soemthing like this.

    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    var future = new CompletableFuture<String>();
    scheduler.schedule(()->future.complete("Data Fetched"), 10, TimeUnit.SECONDS);
    future.thenAccept(result -> System.out.println(result));
    System.out.println("Prints first"); // prints before the async result
    scheduler.shutdown();
wpollock•11mo ago
In Java 24, new features support educational and demonstration use. You don't need a class to wrap your main method, which also has a simpler signature. To compare JavaScript with Java examples, one should make use of these features.

While the examples may need some work, I enjoyed this post, it nicely shows the evolution of Java concurrency.

AtlasBarfed•11mo ago
Does no.js still limit you to a single core/CPU use?

Or as a node successfully been able to start utilizing more cores underneath its JavaScript single thread model. It presents the programmer?

I just remember early node.js from like 15 years ago and the single background task limitation of JavaScript running in a web page.

Cuz you got async code is nice, but what you really wanted to be able to harness in modern CPUs is multi-core

That said, I've been looking for an article like this for a while, although I think there are other associated libraries that also had steps in here. I do think the jvm adopted a lot of those, but I'm not sure if they actually are better than the original extension libraries.

msgilligan•11mo ago
I simplified the first example to:

  void main() {
      CompletableFuture<String> future = CompletableFuture.supplyAsync(this::asyncMethod);
      future.thenAccept(result -> IO.println(result));
      IO.println("Prints first");             // prints before the async result
      future.join();                          // Wait for future to complete
  }

  String asyncMethod() {
      try {
          Thread.sleep(10000);
      } catch (InterruptedException e) {
          return "Interrupted";
      }
      return "Data Fetched";
  }
I made the following changes:

1. Move the asynchronous function called in the CompletableFuture to its own method

2. Use Java 25 "instance main method" (see JEP 25: https://openjdk.org/jeps/512)

3. Use Java 25 IO.println() to simplify console output

4. Instead of throwing a fatal exception on interruption, return "Interrupted" immediately.

5. Use future.join() so the main method waits for the future to complete and the "Data fetched" output is printed.

This program can be run directly from source with `java Example.java`. (If you're using Java 24 or a version of Java 25 prior to EA 22, you need to use `java --enable-preview Example.java`)

Here is a modified version of the example that interrupts the thread:

  void main() {
      ExecutorService executor = Executors.newSingleThreadExecutor();
      CompletableFuture<String> future = CompletableFuture.supplyAsync(this::asyncMethod, executor);
      future.thenAccept(result -> IO.println(result));
      IO.println("Prints first");             // prints before the async result
      executor.shutdownNow();
      future.join();                          // Wait for future to complete
  }

  String asyncMethod() {
      try {
          Thread.sleep(10000);
      } catch (InterruptedException e) {
          return "Interrrupted";
      }
      return "Data Fetched";
  }

Changes in the system prompt between Claude Opus 4.6 and 4.7

https://simonwillison.net/2026/Apr/18/opus-system-prompt/
2•simonw•1m ago•0 comments

Show HN: Open Passkey – open-source passkey auth with free "backendless" host

https://github.com/locke-inc/open-passkey
2•connorpeters•3m ago•0 comments

OpenAI's April 2026 Policy Release: Industrial Policy for the Intelligence Age

https://www.landgate.com/news/energy-intelligence-for-open-ai-s-industrial-policy-blueprint
1•ninjahawk1•14m ago•0 comments

Testing Out the Armips Assembler on the Xorshift PRNGs

https://bumbershootsoft.wordpress.com/2026/04/18/testing-out-the-armips-assembler-on-the-xorshift...
1•ibobev•15m ago•0 comments

Instrumental Insemination of Honey Bee Queens

https://news.cahnrs.wsu.edu/videos/instrumental-insemination-of-honey-bee-queens-techniques-and-b...
2•rolph•20m ago•0 comments

The Unabomber Manifesto in 2026 – Scary Accurate

https://medium.com/@vagobond/reading-the-unabomber-manifesto-in-2026-90c4417fc309
2•ZguideZ•22m ago•1 comments

SEO, AEO, and Geo for a Modern Developer Portfolio

https://www.yashkapure.com/en/blog/seo-aeo-geo-for-a-modern-developer-portfolio/
1•yashkapure•25m ago•0 comments

Show HN: Figma-style visual editor for multi-agent choreography

https://github.com/dance-of-tal/dot-studio
1•monarchjuno•25m ago•0 comments

NASA Shuts Off Instrument on Voyager 1 to Keep Spacecraft Operating

https://science.nasa.gov/blogs/voyager/2026/04/17/nasa-shuts-off-instrument-on-voyager-1-to-keep-...
23•sohkamyung•28m ago•2 comments

AI-Assisted Coding: Why a Distinguished Engineer Stopped Reading Code

https://vascoduarte.substack.com/p/ai-assisted-coding-why-a-distinguished
3•e2e4•32m ago•1 comments

BenchJack – an open-source hackability scanner for AI agent benchmarks

https://github.com/benchjack/benchjack
2•mogician•33m ago•0 comments

The Wired Belts Are the New Rust Belts

https://fletcher.tufts.edu/news-media-mentions/all-news/wired-belts-are-new-rust-belts
1•mitchbob•34m ago•0 comments

Show HN: Jitter Done – a simple tool to understand your caffeine metabolism

https://jitterdone.com/
1•jere•37m ago•1 comments

What if I do not want to provide my cell phone number?

https://www.vendpark.io/faq/what-if-i-do-not-want-to-provide-my-cell-phone-number-new
1•kmstout•41m ago•0 comments

Post-Quantum Cryptography Migration at Meta: Framework, Lessons, and Takeaways

https://engineering.fb.com/2026/04/16/security/post-quantum-cryptography-migration-at-meta-framew...
2•birdculture•45m ago•0 comments

Claude Design System Prompt

https://gist.github.com/mcrowe/9da081f52e0740886d39e730852dba49
3•mcrowe•49m ago•1 comments

Construction delays hit 40% of US data centers planned for 2026

https://arstechnica.com/ai/2026/04/construction-delays-hit-40-of-us-data-centers-planned-for-2026/
1•cratermoon•49m ago•0 comments

A more efficient implementation of Shor's algorithm

https://lwn.net/Articles/1066156/
1•dralley•50m ago•0 comments

ICE Smashed Her Car Windows on the Way to the Doctor. She's Fighting Back[TORT$]

https://www.motherjones.com/politics/2026/04/ice-aliyah-rahman-tort-ntca-dhs/
2•rolph•51m ago•1 comments

Operational Readiness Criteria for Tool-Using LLM Agents

https://zenodo.org/records/19211676
1•rogelsjcorral•53m ago•0 comments

IMDB recent update: user reviews can't be accessed anymore without an account

https://old.reddit.com/r/movies/comments/1soome4/imdbs_very_recent_update_made_it_so_user_reviews/
3•amelius•54m ago•0 comments

New Theorems

https://monogate.org
1•zonked45•55m ago•0 comments

What Used to Be Viivikonna (2021)

https://emptiness.eu/field-reports/what-used-to-be-viivikonna/
1•carlos-menezes•1h ago•0 comments

Figma Make

https://www.figma.com/make/
1•gitgud•1h ago•0 comments

AI as Economic Warfare

https://ghuntley.com/warfare/
3•ganitam•1h ago•1 comments

Kelp DAO Exploited for $292M

https://www.coindesk.com/tech/2026/04/19/2026-s-biggest-crypto-exploit-kelp-dao-hit-for-usd292-mi...
1•scrlk•1h ago•0 comments

Fake Claude site installs malware that gives attackers access to your computer

https://www.malwarebytes.com/blog/scams/2026/04/fake-claude-site-installs-malware-that-gives-atta...
8•ilamont•1h ago•0 comments

KukuOS: A self-hosting, ELF-emitting x86 stack in Kuku Yalanji

https://github.com/australia/kukuos
1•thomasfromcdnjs•1h ago•0 comments

Coding agents and the growing 1% problem

https://try.works/coding-agents-and-the-growing-1-problem
2•try-working•1h ago•0 comments

Claude Code "AUTO MODE" – Not what you think

https://sunglasses.dev/blog/auto-mode-validates-runtime-security
1•azrollin•1h ago•3 comments