Skip to content

Instantly share code, notes, and snippets.

@lnostdal
Created May 2, 2018 17:18
Show Gist options
  • Save lnostdal/fc19dbc9e4c61b6b90d3b94969385be6 to your computer and use it in GitHub Desktop.
Save lnostdal/fc19dbc9e4c61b6b90d3b94969385be6 to your computer and use it in GitHub Desktop.
(let [f (future
(try
(Thread/sleep 10000)
(catch Throwable e
(println e)
(println "isInterrupted:" (.isInterrupted (Thread/currentThread)))
(println "isAlive:" (.isAlive (Thread/currentThread)))
(println "interrupted:" (Thread/interrupted)))))]
(Thread/sleep 500)
(future-cancel f)
nil)
#error {
:cause sleep interrupted
:via
[{:type java.lang.InterruptedException
:message sleep interrupted
:at [java.lang.Thread sleep Thread.java -2]}]
:trace
[[java.lang.Thread sleep Thread.java -2]
[quantataraxia.core$eval66118$fn__66119 invoke form-init2480698204249510623.clj 3]
[clojure.core$binding_conveyor_fn$fn__5564 invoke core.clj 2022]
[clojure.lang.AFn call AFn.java 18]
[java.util.concurrent.FutureTask run FutureTask.java 264]
[java.util.concurrent.ThreadPoolExecutor runWorker ThreadPoolExecutor.java 1135]
[java.util.concurrent.ThreadPoolExecutor$Worker run ThreadPoolExecutor.java 635]
[java.lang.Thread run Thread.java 844]]}
isInterrupted: false
isAlive: true
interrupted: false
@lnostdal
Copy link
Author

lnostdal commented May 2, 2018

quantataraxia.core> (let [f (future 
                              (while (not (.isInterrupted (Thread/currentThread)))
                                (println "hi?")
                                (Thread/sleep 1000)))]
                      (Thread/sleep 3000)
                      (future-cancel f)
                      nil)
hi?
hi?
hi?
nil
quantataraxia.core> ;; uhm, well, that works... 

@lnostdal
Copy link
Author

lnostdal commented May 2, 2018

I should also be catching InterruptedException !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment