The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.



Posted by Matt Gerrans on November 21, 2001 at 5:15 AM

Hi Hiran,

Two points:

  1. I like Brian's suggestion; I think it will make the program simpler and easier to maintain. I believe this is how task scheduling is often done. Another thing I like about it is that in the 99.99% case when there is no task to do, the event handler uses very little CPU. I would augment it a little, by putting all pending tasks in an array sorted by time. Then all tasks whose time is less then or equal to now are executed (and a new task is added if it is a repeating task). This will deal with the case when a task's exact time occurs while the computer is turned off or sleeping.
  2. I assume you are doing your program for Java programming experience and you are aware there is a task scheduler already built into Windows (Accessories/System Tools/Scheduled Tasks)...
  3. and finally, ... oh wait, I only had two points!

- mfg

> Brian, I figured out a solution a bit after posting my question. What I did was to use an int variable that keeps track of how many tasks are actually run at that precise moment. Then, if the variable is greater than 0, I cause the thread to sleep for 60 seconds. The reason for this is that if you look at the code I provided, I was using an endless while loop (which ran as a seperate thread) to continually check if a scheduled task was supposed to run. The problem was that the time specified in the file was the hour and minute, which meant that the while loop would iterate quite a lot of times during the span of that minute. That is why the minute (no pun intended) the current time hits the time of the scheduled task, and the task runs, I pause the thread for 60 seconds, thereby eliminating the task being run multiple times for each iteration within a minute. Also, I do this after the if statement, so that all tasks are run (at least, I think so...haven't tried w/ multiple tasks at the same time yet...should do so now i guess). Anyway, thnx for the suggestion of having a bit that specifies when to do the task next and updating that. Essentially what I'm trying to do is create an application that simplifies scheduling tasks and setting programs to start when the computer starts up. By the way, do you know how to either create an exe file out of a java project, or run the java project automatically (ie, without the project file being double clicked on). The reason I'm asking is because I need to have the install program place my application in the startup folder to run whenever windows starts (I'm gearing this for windows right now). Except that my application will be in byte code (and possibly in a JAR file). So, how do I execute that w/o having the user manually run it using the java.exe program?
> Hiran
> > One possible approach you could take would be to store a "next scheduled run time" in your data file. For example, if you have a task that is to run every Thursday at 9:00am, the next run time would be Thursday, November 22, 2001 9:00am.

> > Then, for each task, compare the current date/time to the next scheduled run time. If the current time is equal to or greater than the next scheduled run time, do two things:
> >

    > >
  • Update the next scheduled run time to Thursday, November 29 2001 9:00am
    > >
  • Start the scheduled task.
    > >

> > If this isn't a viable alternative, we'll see what else we can come up with.

  • Thoughts Hiran November 22, 2001 at 12:25 AM (0)
  • LOL (eom) Chin Loong November 21, 2001 at 6:16 AM (0)

Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us