Clojure STM(dosync)x Java同步块 [英] Clojure STM ( dosync ) x Java synchronize block

查看:108
本文介绍了Clojure STM(dosync)x Java同步块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clojure STM(dosync)方法和Java同步块之间的区别是什么?

What i the difference between Clojure STM ( dosync) approach and Java synchronize Block ?

我从睡眠理发师问题中读取下面的代码。
http ://www.bestinclass.dk/index.clj/2009/09/scala-vs-clojure-round-2-concurrency.html

Im reading the code below from "The sleeping barber" problem. (http://www.bestinclass.dk/index.clj/2009/09/scala-vs-clojure-round-2-concurrency.html)

(defn the-shop [a]  
  (print "[k] entering shop" a)  
  (dosync     
    (if (< (count @queue) seats)  
      (alter queue conj a)  
      (print "[s] turning away customer" a))))

为了避免竞争条件,使用dosync,所以我问自己什么是Java同步块的差异(STM)?是否会阻止这个关键代码?

To avoid race conditions, dosync is used, so i ask myself " What is the diference (STM) from Java synchronize block " ? Is it will block this critical code ?

提前感谢!
Dantas

Thanks in advance ! Dantas

推荐答案

dosync synchronized 允许访问完全不同的并发抽象。

dosync and synchronized give access to completely different concurrency abstractions.

获取和释放锁。当线程进入 synchronized 块时,它尝试获取适当的锁;如果锁当前由不同的线程保持,则当前线程阻塞并等待它被释放。这导致某些问题,例如死锁的风险。当线程退出同步块时,锁定被释放。

synchronized is a way of acquiring and releasing locks. When a thread enters a synchronized block, it attempts to acquire the appropriate lock; if the lock is currently held by a different thread, the current thread blocks and waits for it to be released. This leads to certain problems, such as the risk of deadlock. The lock is released when the thread leaves the synchronized block.

dosync 标记要在事务中运行的代码块。 Clojure中的事务是协调对Refs(使用 ref 函数创建的对象)的更改的一种方法;如果你需要一些代码在Clojure中有一些可变状态的一致视图 - 并且可能会改变它们 - 你把它们放在Refs中并在事务中执行你的代码。

dosync marks a block of code which is to be run in a transaction. Transactions in Clojure are a way of coordinating changes to Refs (objects created with the ref function); if you need some code to have a consistent view of some pieces of mutable state in Clojure -- and possibly change them -- you put those in Refs and execute your code in a transaction.

事务具有令人感兴趣的属性,如果由于某种原因它不能提交,直到某个最大重试次数(当前硬编码为10000),它将重新启动。交易无法提交的可能原因之一是无法获得世界的一致视图(实际上,相关参考文献 - 有一个自适应历史设施,这使得它不是一个问题,乍一看);其他交易同时发生的变更;等等。

A transaction has the interesting property that it will restart if for some reason it cannot commit, up to a certain maximal number of retries (currently hard-coded to be 10000). Among the possible reasons for a transaction being unable to commit are an inability to obtain a consistent view of the world (actually, the relevant Refs -- there is an "adaptive history" facility which makes this less of a problem than it might seem at first glance); simultaneous changes made by other transactions; etc.

事务不会有死锁的风险(除非程序员通过Java互操作程序来引入与STM系统无关的死锁)。活锁,另一方面,是一定的可能性,虽然它不是很可能。一般来说,许多 - 虽然不是全部! - 在STM系统的上下文中,包括Clojure的上下文中,程序员与数据库事务相关的直觉是有效的。

A transaction runs no risk of being deadlocked (unless the programmer goes out of their way to introduce a deadlock unrelated to the STM system through Java interop); livelock, on the other hand, is a certain possibility, though it is not very probable. In general, many -- although not all! -- of the intuitions programmers associate with database transactions are valid in the context of STM systems, including that of Clojure.

STM是一个巨大的话题;一个学习Clojure的STM的优秀资源是Mark Volkmann的软件事务内存文章。它深入讨论Clojure的STM在最后的部分,但开始可以作为伟大的介绍性阅读。

STM is a huge topic; one excellent resource for learning about Clojure's STM is Mark Volkmann's Software Transactional Memory article. It goes into great depth in discussing Clojure's STM in its final sections, but the beginning can serve as great introductory reading.

对于你引用的代码段,它实际上不是东西你通常想在生产代码中模拟,因为 dosync 块应该几乎总是副作用的; print 这里可以用于演示STM的内部工作,但是如果你想要一个事务在实际代码中导致副作用,你应该产生一个Clojure代理程序(仅在事务成功提交时才执行其任务)。

As for the snippet you quoted, it's actually not something you would normally want to emulate in production code, since dosync blocks should almost always be side-effect free; the print here can be useful for demonstrating the inner working of the STM, but if you wanted a transaction to cause side-effects in real code, you should have it spawn a Clojure Agent for the purpose (which would only execute its task if the transaction successfully commits).

这篇关于Clojure STM(dosync)x Java同步块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆