Skip to content

Instantly share code, notes, and snippets.

@borkdude
Created October 11, 2016 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkdude/88c3c92076709538fb394d5eae30050c to your computer and use it in GitHub Desktop.
Save borkdude/88c3c92076709538fb394d5eae30050c to your computer and use it in GitHub Desktop.
ReentrantLock
boot.user=> (def l (java.util.concurrent.locks.ReentrantLock.))
#'boot.user/l
boot.user=> (defn foo [] (future (.lock l) (println "foo is locking") (Thread/sleep 30000) (.unlock l)))
#'boot.user/foo
boot.user=> (defn bar [] (future (if (.tryLock l) (do (println "got the lock") (.unlock l)) (println "passing by"))))
#'boot.user/bar
boot.user=> (foo)
foo is locking
#future[{:status :pending, :val nil} 0x32c734d1]
boot.user=> (bar)
passing by
#future[{:status :pending, :val nil} 0x3501037a]
boot.user=> (bar)
got the lock
#future[{:status :pending, :val nil} 0x2a50aea9]
boot.user=> (.isLocked l)
false
boot.user=>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment