better algorithm?
I have to update a database table every Tues-Sat from 4AM-6AM.
so i written a code for a thread
this is the body of the run() method of a worker class that extends the Thread class.
Calendar calendar = Calendar.getInstance();
int currentHour = calender.get(calendar.HOUR_OF_DAY);
int day = calendar.get(calendar.DAY_OF_WEEK);
int sleepHour= 0;
while (true){
sleepHour = 0;
if (dayBetween(TUESDAY, SATURDAY){// method that determine if current day is between Tues and Saturday
if (currentHour < 4)
sleepHour = 4 - currentHour
elseif (currentHour > 6)
sleepHour = 24-currentHour + 4
else{
performUpdate();
currentHour = calender.get(calendar.HOUR_OF_DAY);
sleep(TWO_HOURS);// make sure it will sleep for two hours after it finished with updating
}
hybernate(sleepHour);
}
else{//it's a SUNDAY or MONDAY
// (TUES - SUNDAY - 1 * 24) + (24 hours in a day - currentHour) + 4 hours on tues morning
sleepHour = (TUESDAY - day - 1) * 24 + (24 - currentHour) + 4;
hybernate(sleepHour);
}
}
publicvoid hybernate(int sleepHour){
if (sleepHour == 0){// sleep for less than an hour b4 perform update
int minutes = calendar.get(Calendar.Minute);
for (int i = 0; i < minutes; i++)
sleep(60 * 1000);
}
for (int i = 0; i < sleepHour; i++){
sleep (60 * 60 * 1000);
}
}
Although the code worked..it's very mesy & clumsy programming.
My Question is..is there a better way of doing this...I positive there is..I just don't know how.
Also, I'm trying to stay away from the Timer class
If you know a better way..please post..or a link that will help me..please post the link.
thanx in advance

