In Java, I am currently working on a UI for a virtual machine. As the user can input their own code to the VM, I am worried about it entering an infinite loop. To prevent the main program (the UI) from being destroyed by an infinite loop in the VM I have placed it in to its own independent thread.
VM_thread= new Thread(this);
source = source_code;
VM_thread.start();
public void run() {
//thread used to containerise the TOY VM
try {
toy_access = new TOY(source);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
As part of this, the user also has/needs the ability to terminate this thread so that it can be restarted. For this I tried using Thread.interrupt() and an internal boolean flag, but both of these failed to terminate the thread when it entered an infinite loop (the boolean flag not being changed). I tried Thread.stop() and this successfully terminated the thread. I have read the articles on the Thread.stop() function saying "other threads may now view these objects in an inconsistent state" etc., but I was wondering if it was safe to do this if all that is contained within the thread is a single object which will be deleted (or not accessible) once the thread has been interrupted?