为什么使用 Clojure 而不是 Java 进行并发编程

发布于 2024-08-08 01:00:06 字数 49 浏览 14 评论 0原文

当Java提供并发编程能力时,使用Clojure(而不是Java)的主要优势是什么?

When Java is providing the capabilities for concurrent programming, what are the major advantages in using Clojure (instead of Java)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

几味少女 2024-08-15 01:00:07

这个视频演示文稿提供了一个非常有力的案例,以高效为中心作为尝试实现的持久数据结构。

This video presentation makes a very strong case, centred around efficient persistent data structures implemented as tries.

掌心的温暖 2024-08-15 01:00:07

Java编程语言的发展相当缓慢,主要是因为Sun对向后兼容性的担忧。

为什么不想直接使用像 Clojure 和 Scala 这样的 JVM 语言呢?

Java programming language evolution is quite slow mainly because of Sun's concern about backward compatibility.

Why don't you want just directly use JVM languages like Clojure and Scala?

空袭的梦i 2024-08-15 01:00:06

Clojure 专为并发而设计。

Clojure 提供了比 Java 更高抽象级别的并发原语。其中一些是:

  • 软件事务内存系统,用于处理对共享引用的同步和协调更改。您可以将多个引用作为原子操作进行更改,而不必担心程序中其他线程正在执行的操作。在您的交易中,您将始终对世界有一致的看法。

  • 用于异步更改的代理系统。这类似于 Erlang 中的消息传递。

  • 线程对变量的本地更改。这些变量有一个根绑定,由程序中的每个线程共享。但是,当您重新绑定变量时,它将仅在该线程中可见。

所有这些并发原语都构建在 Clojures 不可变数据结构(即列表、映射、向量等)之上。当您进入可变 Java 对象的世界时,所有原语都会崩溃,您将回到锁和条件变量(必要时也可以在 clojure 中使用)。

Clojure is designed for concurrency.

Clojure provides concurrency primitives at a higher level of abstraction than Java. Some of these are:

  • A Software Transactional Memory system for dealing with synchronous and coordinated changes to shared references. You can change several references as an atomic operation and you don't have to worry about what the other threads in your program are doing. Within your transaction you will always have a consistent view of the world.

  • An agent system for asynchronous change. This resembles message passing in Erlang.

  • Thread local changes to variables. These variables have a root binding which are shared by every thread in your program. However, when you re-bind a variable it will only be visible in that thread.

All these concurrency primitives are built on top of Clojures immutable data structures (i.e., lists, maps, vectors etc.). When you enter the world of mutable Java objects all of the primitives break down and you are back to locks and condition variables (which also can be used in clojure, when necessary).

热情消退 2024-08-15 01:00:06

虽然不是 Clojure 专家,但我想说的是,Clojure 的主要优点是隐藏了并发编程的许多细节,众所周知,细节决定成败,所以我认为这是一件好事。

您可能想查看这个出色的演示< /a> 来自 Rick Hickey(Clojure 的创建者)关于 Clojure 并发性的内容。编辑:显然 JAOO 已经删除了旧的演示文稿。我还没有找到新的来源。

Without being an expert on Clojure I would say that the main advantage is that Clojure hides a lot of the details of concurrent programming and as we all know the devil is in the details, so I consider that a good thing.

You may want to check this excellent presentation from Rick Hickey (creator of Clojure) on concurrency in Clojure. EDIT: Apparently JAOO has removed the old presentations. I haven't been able to locate a new source for this yet.

匿名。 2024-08-15 01:00:06

因为 Clojure 基于函数式编程范式,也就是说它通过遵循一些简单的规则来实现并发安全

  • 不可变状态
  • 函数没有副作用

程序这样编写的代码几乎具有内置的水平可扩展性,而基于锁的并发机制(如 Java)很容易出现涉及竞争条件、死锁等的错误。

Because Clojure is based on the functional-programming paradigm, which is to say that it achieves safety in concurrency by following a few simple rules:

  • immutable state
  • functions have no side effects

Programs written thus pretty much have horizontal scalability built-in, whereas a lock-based concurrency mechanism (as with Java) is prone to bugs involving race conditions, deadlocks etc.

风柔一江水 2024-08-15 01:00:06

因为世界在过去 10 年里已经取得了进步,而 Java 语言(!= JVM)却发现很难跟上。更现代的 JVM 语言基于新的想法和改进的概念,这使得许多繁琐的任务变得更加简单和安全。

Because the world has advanced in the past 10 years and the Java language (!= the JVM) is finding it hard to keep up. More modern languages for the JVM are based on new ideas and improved concepts which makes many tedious tasks much more simple and safe.

往事风中埋 2024-08-15 01:00:06

拥有不可变类型的最酷的事情之一是大多数内置函数已经是多线程的。简单的“减少”将跨越多个核心/处理器,无需您进行任何额外的工作。

所以,当然你可以使用 Java 进行多线程,但它涉及锁和诸如此类的东西。 Clojure 是多线程的,不需要任何额外的努力。

One of the cool things about having immutable types is that most of the built-in functions are already multi-threaded. A simple 'reduce' will span multiple cores/processors, without any extra work from you.

So, sure you can be multi-threaded with Java, but it involves locks and whatnot. Clojure is multi-threaded without any extra effort.

伴我老 2024-08-15 01:00:06

是的,Java 提供了并发程序所需的所有功能。

打个比方:C 为内存安全程序提供了所有必要的功能,即使有大量的字符串处理。但在 C 语言中,内存安全是程序员的问题。

事实上,分析并发性是相当困难的。最好使用本质上安全的机制,而不是试图预测所有可能的并发危险。

如果您尝试通过添加互锁来使共享内存可变数据结构并发程序安全,那么您就在走钢丝。另外,它在很大程度上是不可测试的。

一种好的折衷方案可能是使用 Clojure 的函数式风格编写并发 Java 代码。

Yes, Java provides all necessary capabilities for concurrent programs.

An analogy: C provides all necessary capabilities for memory-safe programs, even with lots of string handling. But in C memory safety is the programmer's problem.

As it happens, analyzing concurrency is quite hard. It's better to use inherently safe mechanisms rather than trying to anticipate all possible concurrency hazards.

If you attempt to make a shared-memory mutable-data-structure concurrent program safe by adding interlocks you are walking on a tightrope. Plus, it's largely untestable.

One good compromise might be to write concurrent Java code using Clojure's functional style.

两仪 2024-08-15 01:00:06

除了 Clojure 通过不可变数据、变量、引用(和软件事务内存)、原子和代理实现并发的方法之外……它还是一个值得学习的 Lisp。您可以获得 Lisp 宏、解构、一等函数和闭包、REPL 和动态类型 - 以及列表、向量、映射和集合的文字 - 所有这些都建立在与 Java 库的互操作性之上(并且也正在开发一个 CLR 版本)。 )

它与Scheme或Common Lisp并不完全相同,但是如果您想完成计算机程序的结构和解释或者了解 Paul Graham 在他的论文,您可以与 XKCD 的此漫画相关。 ;-)

In addition to Clojure's approach to concurrency via immutable data, vars, refs (and software transactional memory), atoms and agents... it's a Lisp, which is worth learning. You get Lisp macros, destructuring, first class functions and closures, the REPL, and dynamic typing - plus literals for lists, vectors, maps, and sets - all on top of interoperability with Java libraries (and there's a CLR version being developed too.)

It's not exactly the same as Scheme or Common Lisp, but learning it will help you if you ever want to work through the Structure and Interpretation of Computer Programs or grok what Paul Graham's talking about in his essays, and you can relate to this comic from XKCD. ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文