Hi all,

I have recently been playing with futures and have stumbled on the 
following situation.

(defn test-f1 [initial-v]
  (let [n (atom initial-v)]
    [n
     (future (while (not (Thread/interrupted))
               (Thread/sleep 5000)
               (swap! n inc)
               (println @n)))]))

(def t1 (test-f1 11))

This works as expected.  I see the println outputs, can @(t1 0) and 
(future-cancel (t1 1)) returns true.

Then I have:

(defn test-f2 [initial-v]
  (let [n (atom initial-v)]
    [n
     (future (while (not (Thread/interrupted))
               (try
                 (Thread/sleep 5000)
                 (swap! n inc)
                 (println @n)
                 (catch Exception e (println "whoops")))))]))

(def t2 (test -f2 11))

The same except now my while has a (try ... (catch Exception e ...). 
 However, now (future-cancel (t2 1)) returns false and the future cannot be 
cancelled - I also see the "whoops" at the REPL.  Seeing that "whoops" made 
me wonder if the interrupt that would shutdown the thread is raising an 
exception that is being caught by my (try .... (catch ...)) block, thus 
never interrupting the thread. 

I made a change to throw the exception again:

(defn test-f3 [initial-v]
  (let [n (atom initial-v)]
    [n
     (future (while (not (Thread/interrupted))
               (try
                 (Thread/sleep 5000)
                 (swap! n inc)
                 (println @n)
                 (catch Exception e (do (println "whoops") (throw 
e))))))])) 

and the behavior now matches that of test-f1.

I think I'm just not clear on some fundamental aspect of how future-cancel, 
try/catch, and the underlying java are actually working here.

It seems *very* weird to me that after future-cancel cancels the future, 
that the (try . . . (catch . . .)) would ever be invoked in the body of the 
while loop.  Yet, clearly the interrupt is being handled in the (try . . . 
(catch . . . )).  It also seems *very* weird that even in this case, that 
the next time we get back to (while (not (Thread/interrupted)) that 
(not(Thread/interrupted)) would be true . . . I guess the exception sets 
the interrupt status for the thread.  When exception gets caught in my (try 
. . . (catch . . .)), the interrupt status is never set?

Thanks for any clarification.
 

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to