I have an application having a thread which runs continously,writing to a file.And it should terminate on a button click.But this results in flooding of Exceptions which forces the application to terminate.I tried using stop() function,but that doesn’t work.Can any one suggest a method(code if possible) to safely terminate the thread and close the file.
I have an application having a thread which runs continously,writing to a file.And it
Share
Threading in Java is cooperative: you can not stop a thread from outside, you can only signal it to stop an then thread stops itself (i.e. exits the run method).
To signal it to stop, just have a boolean field
running=truewhich thread checks in it’s main loop. Whenrunning==falsethread exits therun()method.In your particular case it could be a bit trickier depending how you read data to be written to file. Do you use any blocking read calls to read the data?