TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Understanding Java's Asynchronous Journey

17 点作者 hardasspunk24 天前

5 条评论

stevoski24 天前
The Java 1 example uses lambdas, which were introduced in Java 8.<p>It’s probably intentional, because it allows showing the Java 1 Thread approach succinctly.<p>But as long-term Java person, I find it jarring.
msgilligan24 天前
I simplified the first example to:<p><pre><code> void main() { CompletableFuture&lt;String&gt; future = CompletableFuture.supplyAsync(this::asyncMethod); future.thenAccept(result -&gt; IO.println(result)); IO.println(&quot;Prints first&quot;); &#x2F;&#x2F; prints before the async result future.join(); &#x2F;&#x2F; Wait for future to complete } String asyncMethod() { try { Thread.sleep(10000); } catch (InterruptedException e) { return &quot;Interrupted&quot;; } return &quot;Data Fetched&quot;; } </code></pre> I made the following changes:<p>1. Move the asynchronous function called in the CompletableFuture to its own method<p>2. Use Java 25 &quot;instance main method&quot; (see JEP 25: <a href="https:&#x2F;&#x2F;openjdk.org&#x2F;jeps&#x2F;512" rel="nofollow">https:&#x2F;&#x2F;openjdk.org&#x2F;jeps&#x2F;512</a>)<p>3. Use Java 25 IO.println() to simplify console output<p>4. Instead of throwing a fatal exception on interruption, return &quot;Interrupted&quot; immediately.<p>5. Use future.join() so the main method waits for the future to complete and the &quot;Data fetched&quot; output is printed.<p>This program can be run directly from source with `java Example.java`. (If you&#x27;re using Java 24 or a version of Java 25 prior to EA 22, you need to use `java --enable-preview Example.java`)<p>Here is a modified version of the example that interrupts the thread:<p><pre><code> void main() { ExecutorService executor = Executors.newSingleThreadExecutor(); CompletableFuture&lt;String&gt; future = CompletableFuture.supplyAsync(this::asyncMethod, executor); future.thenAccept(result -&gt; IO.println(result)); IO.println(&quot;Prints first&quot;); &#x2F;&#x2F; prints before the async result executor.shutdownNow(); future.join(); &#x2F;&#x2F; Wait for future to complete } String asyncMethod() { try { Thread.sleep(10000); } catch (InterruptedException e) { return &quot;Interrrupted&quot;; } return &quot;Data Fetched&quot;; }</code></pre>
AtlasBarfed24 天前
Does no.js still limit you to a single core&#x2F;CPU use?<p>Or as a node successfully been able to start utilizing more cores underneath its JavaScript single thread model. It presents the programmer?<p>I just remember early node.js from like 15 years ago and the single background task limitation of JavaScript running in a web page.<p>Cuz you got async code is nice, but what you really wanted to be able to harness in modern CPUs is multi-core<p>That said, I&#x27;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&#x27;m not sure if they actually are better than the original extension libraries.
Neywiny24 天前
I don&#x27;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:<p>A completable future is something that in the future may complete. I think that&#x27;s self explanatory. A promise seems equally vague.<p>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&#x27;ll take all I can get.<p>API is eh. Sure. But that&#x27;s not even shown in this example so I have no idea.<p>So JS saves like 3 lines? Is that really so much better?
评论 #43976543 未加载
评论 #43974341 未加载
philipwhiuk24 天前
Java&#x27;s had `var` since Java 10 but apparently the author deliberately ignored that to make the example as wordy as possible.<p>It&#x27;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.<p>If you strip that out the original Java is:<p><pre><code> var future = CompletableFuture.supplyAsync(() -&gt; { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } return &quot;Data Fetched&quot;; }); future.thenAccept(result -&gt; System.out.println(result)); System.out.println(&quot;Prints first&quot;); &#x2F;&#x2F; prints before the async result </code></pre> which is only obtuse due to checked exceptions.<p>Arguably it&#x27;s still a different thing you&#x27;re doing, because it&#x27;s not scheduling a task on a pool, it&#x27;s creating a thread which sleeps for 10 seconds.
评论 #43974302 未加载
评论 #43974290 未加载
评论 #43974547 未加载