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.


Figured it out!...Here's what I did

Posted by Hiran on November 21, 2001 at 3:47 AM

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?

> 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.


Sponsored Links

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