简体   繁体   English

Java与Clojure的良好并发示例

[英]Good concurrency example of Java vs. Clojure

Clojure is said to be a language that makes multi-thread programming easier. 据说Clojure是一种使多线程编程更容易的语言。

From the Clojure.org website: 来自Clojure.org网站:

Clojure simplifies multi-threaded programming in several ways. Clojure以多种方式简化了多线程编程。

Now I'm looking for a non-trivial problem solved in Java and in Clojure so I can compare/contrast their simplicity. 现在我正在寻找在Java和Clojure中解决的非平凡问题,因此我可以比较/对比它们的简单性。 Anyone? 任何人?

I'd suggest also looking at Christophe Grand's thread safe blocking queue ; 我建议也看一下Christophe Grand的线程安全阻塞队列 ; it's less than 20 lines, but packs a lot of functionality, and in my opinion demonstrates expert use of some of Clojure's concurrency features, immutability, atoms, and lazy sequences. 它不到20行,但包含了很多功能,在我看来,它展示了Clojure的一些并发特性,不变性,原子和惰性序列的专家使用。

Consider that the Java alternative java.util.concurrent.LinkedBlockingQueue is 842 lines of subtle (arguably complex) commented code, and you begin to understand how Clojure really does deliver on its concurrency promise; 考虑到Java替代java.util.concurrent.LinkedBlockingQueue 是842行细微(可称复杂)注释代码,您开始理解Clojure如何真正实现其并发承诺; significantly raising the level of abstraction and delivering a correct implementation with approximately 10-20x less code. 显着提高抽象级别并提供正确的实现,代码大约减少10-20倍。

You'll also notice that the when reading the Java code it is really hard to see the forest for the trees... If you were given it could you be sure of its correctness by looking at it? 您还会注意到,在阅读Java代码时,很难看到树木的森林......如果您获得了它,您可以通过查看它来确定它的正确性吗? Also bear in mind that this code was written by Doug Lea (arguably the foremost expert on Java Concurrency) and is for java highly readable; 还要记住,这段代码是由Doug Lea(可以说是Java Concurrency最重要的专家)编写的,并且对于java具有高度可读性; I very much doubt I could write readable performant code such as this in Java quickly and be sure of its correctness. 我非常怀疑我能用Java快速编写可读的高性能代码,并确保其正确性。

Contrast this to the Clojure version and once familiar with the basics of Clojure it's easy to tease apart and understand how it works... Within 20 minutes I was able to understand every line of the implementation, and be sure of its correctness. 与Clojure版本形成对比,一旦熟悉Clojure的基础知识,就很容易分开并理解它是如何工作的...在20分钟内,我能够理解实现的每一行,并确保其正确性。 And now that I'm far more familiar with Clojure's idioms and FP, I'd guess this would now take me closer to 5 minutes. 而现在我对Clojure的成语和FP更为熟悉了,我猜这将使我接近5分钟。 I'd also probably be able to write "correct" code like this in Clojure in hours or minutes. 我也许可以在几小时或几分钟内在Clojure中编写这样的“正确”代码。

Christophes clojure wrapper of the above java class is also instructive as it shares the same functional interface as the first version. 上述java类的Christophes clojure包装器也具有指导性,因为它与第一个版本共享相同的功能接口。

The Wide Finder Project , started by Tim Bray, has a number of Clojure entries -- the most notable of which is by Alex Osborne; 由Tim Bray创办的Wide Finder项目有许多Clojure项目 - 其中最引人注目的是Alex Osborne; he's done a fantastic write-up on it -- as well as entries in Java, Scala and an impressive number of other languages. 他对它做了很棒的写作 - 以及Java,Scala和其他令人印象深刻的语言的条目。

The problem being solved is thoroughly practical and rather interesting as a parallelisation challenge and Tim's postings about it (see also the more recent Wide Finder 2 series) are quite enjoyable to read (and carry a good educational value). 正在解决的问题非常实用且相当有趣,因为并行化挑战和蒂姆关于它的帖子(另见最近的Wide Finder 2系列)阅读非常愉快(并且具有良好的教育价值)。 Plus Alex's text is really great, please read it even if you decide to skip the rest. 另外亚历克斯的文字真的很棒,即使你决定跳过其余部分,请阅读它。 It uses some Clojure-specific features (like Atoms) alongside stuff brought over from Java were it does the job fine & fast... In fact, it would be interesting just for the way in which it showcases Clojure's excellent Java interop. 它使用了一些Clojure特有的功能(比如Atoms)以及从Java带来的东西,它可以很好地和快速地完成工作......事实上,它只是展示Clojure优秀的Java互操作的方式会很有趣。

All in all, I'd say this is likely the best thing to start with. 总而言之,我认为这可能是最好的开始。

请查看Rich Hickey的蚂蚁示例 - 它展示了如何使用clojure的功能来构建并发程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM