I have a multi threaded console application which runs endlessly. Right now I use ctrl-c to end it, but sometimes it causes the finally block not to be called and leaves some processes that the original program started to run even after the main java program has ended.
I found this answer on Stackoverflow
https://stackoverflow.com/a/3194586/492015
class MyThread extends Thread
{
volatile boolean finished = false;
public void stopMe()
{
finished = true;
}
public void run()
{
while (!finished)
{
//do dirty work
}
}
}
But since my program is console based, how would this apply? I would need to make the program wait for keyboard inputs? so if I press 'q' for example stopMe() would get called. How would I have to do this?