目录

dosync

在包含表达式和任何嵌套调用的事务中运行表达式(在隐式do中)。 如果此线程上尚未运行任何事务,则启动事务。 任何未捕获的异常都将中止事务并从dosync流出。

语法 (Syntax)

以下是语法。

(dosync expression)

Parameters - 'expression'是表达式的集合,它将出现在dosync块中。

Return Value - 无。

例子 (Example)

以下程序显示了如何使用它的示例。

(ns clojure.examples.example
   (:gen-class))
(defn Example []
   (def names (ref []))
   (defn change [newname]
      (dosync
         (alter names conj newname)))
   (change "John")
   (change "Mark")
   (println @names))
(Example)

输出 (Output)

上述程序产生以下输出。

[John Mark]
↑回到顶部↑
WIKI教程 @2018