Stops a thread that can loop forever

I have a program in which I compile Java code that the user enters into a text box and then runs it. Run the code in a separate thread so that the graphical interface that they use to enter the source code is not blocked.

The GUI has an interrupt button that should stop the thread. My problem is that I need to stop the compilation stream no matter what happens inside it, which means that I have to consider the case when the stream falls into an infinite loop (due to a user error) and it cannot finish correctly using safe flag. I read a lot of solutions that involve using some kind of flag, but they are not available to me because of this loop problem. I need to stop the thread and free the memory that I use (I can't just let it sit in the background forever, unless that remains the only solution). Any tips or alternative solutions? Hope some fresh perspectives can help solve this problem.

Edit:

Here's an example bit of user-submitted code:

public class RunMe extends SomethingThatRuns { public void run() { int i = 0; while (i = 0) { //Prepare to get stuck! } } } 

I will compile this class and run it. Here it gets stuck, and the run() method can never end or even execute a loop to check the flag.

+4
source share
6 answers

You can run it in the new JVM so that you can kill it whenever you want. Thinking about security, this can also be useful.

+2
source

Call stop () on the stream.

Yes, this is an obsolete method. However, it really should not be "obsolete", it should be "dangerous". However, in some cases there is no choice but to use it, and calling the “agent” provided by the user is one such case.

Make sure your program does not use data that is processed by this user stream; or, if you do, design a transaction mechanism to securely exchange data between threads.

Even this method does not guarantee termination of the flow. For example, a user can catch the received Throwable and ignore it. Or, a thread implementation may not respond to stop() calls if the thread is in some native code. But this is your best chance.

+2
source

The main problem here is that the code even allows you to enter an infinite loop as part of a user error. Fix it and everything else will become easier to deal with.

Correctly acting threads should tend to cease gracefully when there is no work (or calmly return to the thread pool to ask for more work if this is your application design). If you feel that you need one thread to kill another, you probably got a fundamental design problem. It’s good that one thread said to the other: “Hey, you have to finish now so I can join you ...” because it allows your threads to clean things as they are completed. Strongly destroying threads is not the right way to manage these situations.

+1
source

You can use them to insert mutual validation into each loop, and possibly elsewhere.

I see two options:

  • When you compile the user code, you can edit it earlier. You can use ANTLR to analyze and modify code.
  • Bytecode manipulation mechanisms exist, such as ASM, that allow you to manipulate code that is already compiled.

I don’t think it’s easy, but it could be a way.

+1
source

interupt(); Stream in gui

and in the code that the thread regularly checks for Thread.interrupted() and throws an exception when you do especially inside loops

0
source

At a high level, you ask how one thread can stop another thread from stopping. To this end, see this SO question. Stopping a thread in Java? .

0
source

All Articles