Thread testing for time
Posted
by DanielFH
on Stack Overflow
See other posts from Stack Overflow
or by DanielFH
Published on 2010-04-29T08:01:24Z
Indexed on
2010/04/29
8:07 UTC
Read the original article
Hit count: 287
Hi there :) I'm making a thread for my application that's going to do an exit operation at a given time (only hours and minutes, day/month doesn't matter). Is this the right way to do it, and also the right way to test for time? I'm testing for a 24 hour clock by the way, not AM / PM.
I'm then in another class going to call this something like new Thread(new ExitThread()).start();
public class ExitThread implements Runnable {
@Override
public void run() {
Date date = new Date(System.currentTimeMillis());
String time = new SimpleDateFormat("HHmmss").format(date);
int currentTime = Integer.parseInt(time);
int exitTime = 233000;
while(true) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(currentTime >= exitTime ) {
// do exit operation here
}
}
}
Thanks.
//D
© Stack Overflow or respective owner