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

[3049 byte] By [tnguyen1973a] at [2007-9-29 15:38:56]
# 1
Why are you staying away from the Timer class?
dubwaia at 2007-7-15 13:36:01 > top of Java-index,Other Topics,Algorithms...
# 2
I'm not familiar with that class. And kinda busy with school project and work projects to learn it.btw..I'm a slow learner.
tnguyen1973a at 2007-7-15 13:36:01 > top of Java-index,Other Topics,Algorithms...
# 3

I don't get this:

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);

}

Are you setting the calendar minutes anywhere? As far as I can tell you are sleeping for as many minutes have passed in the current hour when the run method executes. So if it's 2:05 when this runs, it will sleep for 4 minutes, and if it's 8:59 it will sleep for 59 minutes. If it's 12:00 it doesn't sleep at all.

dubwaia at 2007-7-15 13:36:01 > top of Java-index,Other Topics,Algorithms...
# 4
it's just incase i have somehow end up with the sleepHour == 0, but it's 3:30AM..i don't want the thread to be active and consume the processor
tnguyen1973a at 2007-7-15 13:36:01 > top of Java-index,Other Topics,Algorithms...
# 5

> I'm not familiar with that class.

> And kinda busy with school project and work projects

> to learn it.

I'm not saying that it's neccesary to use it. I just wanted to know why you are avoiding it.

Basically, there are two ways to go about doing this. One is you figure out how long you have to wait and sleep that long. The other is that you schedule tasks and then check every so often to see if it's time to execute a task. The latter it generally prefferable. You are doing the former.

The Timer class is really pretty simple. There isn't much to learn. You create a TimerTask and tell the timer to execute it either once or repeatedly. There is also an initial delay and a period.

In your case you could create a TimerTask and if it's a day you are not supposed to run, don't do anything. Then you can just schedule it to execute every day.

Another way is to repeatedly schedule the task to run once at a specific time. Everytime it runs it could schedule the next run or you can have a separate task that does scheduling.

dubwaia at 2007-7-15 13:36:01 > top of Java-index,Other Topics,Algorithms...
# 6
oops..i forgot one line that calculate the minutes minutes = 60 - minute;
tnguyen1973a at 2007-7-15 13:36:01 > top of Java-index,Other Topics,Algorithms...
# 7
i'm loking at the tutorial right now, and yes..it doens't seem hard.I guess the more you learn and mees with java..the the easiar learing some new feature.I recall the Timer was a tough topic for me when I first learn java. thanx
tnguyen1973a at 2007-7-15 13:36:01 > top of Java-index,Other Topics,Algorithms...
# 8
Hi,If you want a real timebased scheduling, you could take a look at Quarz. http://sourceforge.net/projects/quartzWith that you could schedule whenever you wwant, and do not even have to change your code if you want to change the time.Eth
ethrandila at 2007-7-15 13:36:01 > top of Java-index,Other Topics,Algorithms...