The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

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.

Message:

Thoughts

Posted by Hiran on November 22, 2001 at 12:25 AM

Thnx for the suggestion Matt. I like the idea of executing tasks that should've been executed but weren't because the comp was alseep or off. Except, how would I make sure that every task isn't executed? Should I have an entry that specifies the last time the task was run, and then compare that with the frequency of when the task is supposed to be run? Secondly, I know that Windows has a task manager. I'm not quite sure why I'm doing this. I guess partly for programming experience. Partly cause I either have all these ideas for great applications but I never do anything beyond thinking about how they would work, or I start to create them and then eventually give up. I guess I just want to create an app fully. I'm thinking of scrapping the task manager part though and just having the first part of my program (which is to allow users to specify programs that run on startup). It's just that the more I think about it, the more I realize that Window's task scheduler seems to "cover all the points" and unless I can think of something that I can add to task scheduling, there's probably no point in me re-inventing the wheel.
Hiran

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

>






Replies:

Sponsored Links



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