11.2 线程
优质
小牛编辑
128浏览
2023-12-01
线程
所有的Clojure方法都实现了 [java.lang.Runnable](http://java.sun.com/javase/6/docs/api/java/lang/Runnable.html)
接口和 [java.util.concurrent.Callable](http://java.sun.com/javase/6/docs/api/java/util/concurrent/Callable.html)
接口。这使得非常容易把Clojure里面函数和java里面的线程一起使用。比如:
(defn delayed-print [ms text]
(Thread/sleep ms)
(println text))
; Pass an anonymous function that invokes delayed-print
; to the Thread constructor so the delayed-print function
; executes inside the Thread instead of
; while the Thread object is being created.
(.start (Thread. #(delayed-print 1000 ", World!"))) ; prints 2nd
(print "Hello") ; prints 1st
; output is "Hello, World!"