Need a Java based interruptible timer thread
Posted
by LambeauLeap
on Stack Overflow
See other posts from Stack Overflow
or by LambeauLeap
Published on 2010-05-18T23:02:44Z
Indexed on
2010/05/18
23:40 UTC
Read the original article
Hit count: 202
I have a Main Program which is running a script on the target device(smart phone) and in a while loop waiting for stdout messages. However in this particular case, some of the heartbeat messages on the stdout could be spaced almost 45secs to a 1minute apart.
something like:
stream = device.runProgram(RESTORE_LOGS, new String[] {});
stream.flush();
String line = stream.readLine();
while (line.compareTo("") != 0) {
reporter.commentOnJob(jobId, line);
line = stream.readLine();
}
So, I want to be a able to start a new interruptible thread after reading line from stdout with a required a sleep window. Upon being able to read a new line, I want to be able to interrupt/stop(having trouble killing the process), handle the newline of stdout text and restart a process.
And it the event I am not able to read a line within the timer window(say 45secs) I want to a way to get out of my while loop either.
I already tried the thread.run, thread.interrupt approach. But having trouble killing and starting a new thread.
Is this the best way out or am I missing something obvious?
© Stack Overflow or respective owner