Killing infinite loops in Java / Clojure

Answers to some "potential answers"

  • You must sprinkle "interrupts" in your threads

    I am not writing my code with the intention of being a long process / infinite loop; it's just that during the development process, I accidentally write code that turns out to be an infinite loop, so I never plan in advance to put a "check if the stream was interrupted" in the code.

Question:

Acquainted with the development of Java / Clojure / Swank and incremental code. It is very easy for me to accidentally write a clojure function that ends with an infinite loop - and start it. Then it goes on and binds the JVM, forcing the fans on my laptop to spin - and basically I have to kill the whole JVM just to get rid of one running thread.

Now, is it possible to somehow kill these clojure themes safely? I am well aware that Thread.stop has various unsafe consequences (for example, blocking locks that other threads might need, etc.), however here are the following clojure functions that are infinite loops - and I do them outside of any STM - so I'm wondering if there is a way to kill these threads safely.

Thank!

+5
source share
4 answers

I do not think that there is a completely complete answer, although there are some incomplete, but still useful things:

  • First, I hit ctrl-c ctrl-cfrom repl, which kills the foreground thread, which receives 99% of my common errors.
  • Then, if that fails, I go for the terminal and the team kill.
  • M-x slime-quit-lisp, clojure-jack-in
+7

, , -, , , /tmp. .

+2

, , , .

...

, clojure, ., JPS - , JPS Java, , clojure.

JPS , Java. ... , , , "" .

doolittle-5:~ Jpeerindex$ jps -l
61133 jline.ConsoleRunner
58998 start.jar
61161 sun.tools.jps.Jps
51866 jline.ConsoleRunner

, "lein repl" (clojure repl) jline ( ConsoleRunner), .

, jstack:

$ > jstack 51866

" № 0 ( GC)" prio = 9 tid = 101802800 nid = 0x1017f9000 runnable

"- № 1 ( GC)" prio = 9 tid = 101803800 nid = 0x102301000 runnable

" GC-" prio = 9 tid = 10184e000 nid = 0x1093f0000 runnable " VM" prio = 10 tid = 1018a4000 nid = 0x10a310000

" " prio = 10 tid = 101802000 nid = 0x100704000 JNI: 137

, - , ..

0

Eclipse/Counterclockwise beta ( ) REPL. , .

It uses NREPL 0.2.0 beta, I think through clojure.tools.nrepl.middleware.interruptible-eval. Not only the Eclipse / CCW NREPL 0.2.0 client, but also the latest version of REPL-y should support this function (by default CTRL-C to stop the stream, CTRL-D to stop REPL).

0
source

All Articles