Complete Insoluble Thread

I created a java application and have a thread that does something in the background when a button is clicked. The problem is that the thread may be blocked, possibly due to an infinite loop. Is there a way I can get this thread to end?

EDIT: I am using LuaJ on the Java platform. It has a blocking potential, and in fact, I don’t have much control over it other than launching it in another thread and destroying it when java or script ends.

+3
source share
2 answers

No no. Your only choice without major witchcraft is to cause it to interrupt () and wait until it hits a blocking call. At this point, it throws an InterruptedException, which was supposed to interrupt the thread.

If it is in a cycle such as while(1);, you cannot do much; you need to fix your flow so you don’t do this, or at least check the flag and stop yourself.

For example, you can rewrite:

while(1) {
    doWork();
}

as:

class Whatever {
    private volatile boolean stop = false;
    ...
        while (!stop) {
            doALittleWork();
        }
    ...
}

There used to be a Thread.stop()method that does this, but it is deprecated for very, very good reasons and should not be used.

EDIT: , ( ) stop, Lua. , Lua "terminate", - , , , , .

, Lua4j, . , , Thread - .

+6

, Java V5.0 ( V1.5), java.util.concurrent, . : , ().

0

Source: https://habr.com/ru/post/1766838/


All Articles