Clojure STM ( dosync ) x Java synchronize block

后端 未结 4 1652
广开言路
广开言路 2021-02-14 05:36

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

I\'m reading the code below from \"The sleeping barber\" problem. (http://www.b

4条回答
  •  轮回少年
    2021-02-14 06:05

    basic difference is following

    Clojure STM supports optimistic concurrency whereas JAVA synchronized is pessimist

    Clojure STM does not acquire lock until there are more than one thread. if mutable state is updated by another thread then the operation inside dosync is repeated. Also, dosync is mandatory for mutable states. Clojure throws illegalState exception when dosync is missing, unlike JAVA.

提交回复
热议问题