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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

The Java Synchronisers

43 点作者 javinpaul将近 9 年前

3 条评论

Freak_NL将近 9 年前
Concurrency is one of the harder bits to learn to do properly, but the CountDownLatch always strikes me as one of the concurrency constructs that is intuitive to grasp.<p>Because these are usually declared <i>final</i> in Java in order to use them in a Thread launched from another class, I just can&#x27;t seem to resist putting this comment above their declaration:<p><pre><code> &#x2F;* It&#x27;s the *&#x2F; final CountDownLatch latch = …</code></pre>
评论 #12383541 未加载
bluejekyll将近 9 年前
Concurrency is hard, try to avoid anything that makes it harder. A lot of the functional languages have something we can all learn from here. For Java I recommend this:<p>- always make your class fields final, if they must be non-final, question why and decide what will happen across threads.<p>- use executors and futures instead of rolling your own threads. Most, if not all, concurrency patterns can be built around these.<p>- when all else fails and you need the primitives from this article, make sure you read <a href="https:&#x2F;&#x2F;books.google.com&#x2F;books&#x2F;about&#x2F;Java_Concurrency_in_Practice.html?id=EK43StEVfJIC" rel="nofollow">https:&#x2F;&#x2F;books.google.com&#x2F;books&#x2F;about&#x2F;Java_Concurrency_in_Pra...</a> before writing a line of concurrent code.<p>By the way, that book is all about Java, but is excellent at getting across concepts that a practical for general concurrency best practices.
评论 #12388443 未加载
ninjakeyboard将近 9 年前
Java8 helps a lot - CompletableFutures can be easier to reason about IMO. If you need to share state, you can use Akka to encapsulate the state in an actor, and thus you will have (what appears to be) lock free concurrency!