It's not safe if the thread is holding any locks. It *may* also leak native
resources if the thread is holding those, but native resources held by a
heap-allocated Java object are supposed to be cleaned up by the finalizer
if the object is GC'd, and I think Thread.stop properly removes that
thread's locals as root set objects for GC, so native leaking would only
happen if the thread held native resources directly in locals, which would
only happen if it was executing a native method at the time of the stop. I
don't know if the JVM/JNI has a safeguard against native leaks from threads
being aborted while in native code, but I'd be mildly surprised if it did.

Clojure threads will potentially be holding locks if they are using
locking, dosync, swap!, or pretty much any of the concurrency primitives in
Clojure. That *might* include Var lookups; I'm not sure (*dynamic* var
lookups involve ThreadLocal, which might use locks under the hood). Many
Java objects use locks somewhere under the hood as well -- certainly
everything in j.u.concurrent is suspect in that regard (and therefore,
swap! and many other Clojure concurrency primitives).

I'd be very leery of playing around with Thread.stop in any circumstance
more complicated than the thread's .run method is doing a pure math loop or
something similar. If it touches Java libraries (outside of
java.lang.String, java.math, and other value types) or uses Clojure
primitives (and how is it supposed to join its results back into the bigger
picture without them?) then it's dangerous. If it is a tight loop of math
stuff then you can check for the interrupted flag.

My recommendation? Stay far, far away from Thread.stop (and .suspend) and
sprinkle Thread.sleep(1)s here and there in the math (maybe every certain
number of iterations -- a millisecond is still a LONG time compared to
primitive arithmetic ops). That should cause the thread to die with an
InterruptedException if .interrupt is called on it. If the thread does any
blocking I/O (or blocking core.async/j.u.concurrent stuff) with any
frequency it should also go tits up pretty quickly if .interrupted.


On Wed, Jan 22, 2014 at 4:31 PM, Mark Engelberg <mark.engelb...@gmail.com>wrote:

> So I guess this gets back to my earlier question: when is it safe to
> terminate a thread?
>
> I know that I often hit Ctrl-C in the REPL to terminate a long running
> function, and I've never really worried about it screwing things up.
>
>
> On Wed, Jan 22, 2014 at 1:29 PM, Shantanu Kumar 
> <kumar.shant...@gmail.com>wrote:
>
>>
>>
>> On Thursday, 23 January 2014 02:37:43 UTC+5:30, puzzler wrote:
>>>
>>> Is there a convenient way within Clojure to launch a Clojure function or
>>> Java call in a separate process as opposed to a separate thread?  Only way
>>> I know of is to literally shell out to the command prompt and launch a new
>>> executable.
>>>
>>
>> There's ProcessBuilder and Runtime.exec stuff, but it will have the JVM
>> and Clojure initialization overhead anyway.
>>
>> http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
>>
>> http://www.tutorialspoint.com/java/lang/runtime_exec_envp.htm
>>
>> Shantanu
>>
>> --
>> --
>> 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/groups/opt_out.
>>
>
>  --
> --
> 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/groups/opt_out.
>

-- 
-- 
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/groups/opt_out.

Reply via email to