Created
October 11, 2016 19:51
-
-
Save borkdude/88c3c92076709538fb394d5eae30050c to your computer and use it in GitHub Desktop.
ReentrantLock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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