Using TimerTask to run a process 3 times (one at 0 seconds, one at a specified time , and another at a third time)

Hi

How would I run a process 3 times using TimerTask where I can pass in 3 different times.  I think I would place what I want to kick off in the run process, but how would I return notice bak to the user to let them know if the process complete or not?

Thanks
ryno71
ryno71Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
I'm not sure if you can specify timer tasks using the same timer with 3 different times. Perhaps you can simply write your own thread and do the task (and use your own time for sleeping). Something like:

int[] durations = { 500, 100, 200 } ;
int[] processes = { "Process1", "Process2", "Process3" } ;

In your run () method:

int count = 0 ;

while ( count < durations.length )
{
  Process p = Runtime.getRuntime ().exec ( process[count] ) ;
  p.waitForExit () ;
 // do any other stuff you want to do
  Thread.sleep ( count ++ ) ;
}
a122178Commented:
System.out.println("Process 1 is done");

Then check out the print out message on the console.
ryno71Author Commented:
mayankeagle

If I just wanted to have it run within one thread and not stop it

and only execute only under on type of condition that once set wont do it

public static void main(String args[])
{
while (threadtoggle=true)
{
 if scenario is true
  run method



}

}
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ryno71Author Commented:
mayankeagle

If I just wanted to have it run within one thread and not stop it

and only execute only under on type of condition that once set wont do it

public static void main(String args[])
{
while (threadtoggle=true)
{
 if scenario is true
  run method

Scenario stil true wait said time

run method

Scenario true

Run method


continue now that it passed or fail out (threadtoggle =false)



}

}
Mick BarryJava DeveloperCommented:
  Timer timer = new Timer();
    timer.schedule(task1, new Date());    // execute now
    timer.schedule(task2, time1);             // execute at time1
    timer.schedule(task3, time2);             // execute at time2
Mayank SAssociate Director - Product EngineeringCommented:
In each of the tasks defined above as objects has shown, you will need to check if the scenario is still true or not, at the starting of the method.
ryno71Author Commented:
Sorry if I wasn't clear in my previous posting. Don't particularly like Mondays!  I increased the points to this one too

This is basically what I want to do

have only one timer thread...

want to have the three task try if a scenario is present if not do other processing.  Is there a way to capture the result
of a scheduled task if it fails ===> timer.schedule(task2, time1);             // execute at time1
Should I try cand catch them to do that??????  



Timer timer = new Timer();
testboolean is false

while (logic=true)  // want this as a constant process….
{

     while (testboolean is false) //to test if scenario is present
      {

       
         boolean testboolean false
{  
timer.schedule(task1, new Date());    // execute now
}
       

process completes
testboolean true

process fails
testboolean false
try again (testboolean still false)
{
timer.schedule(task2, time1);             // execute at time1
}

process completes
testboolean true

process fails again
testboolean false
try again
{
timer.schedule(task3, time2);  
}

completes
testboolean true


fails for the thirs time
testboolean still false
sleep (1day)

}

other processing

}


thanks
ryno71
Mayank SAssociate Director - Product EngineeringCommented:
If you want just one thread and you don't want to schedule tasks in the future, then I would say you don't need a Timer but you can do it in a simplified manner by using your own thread.

ryno71Author Commented:
Actually mayankeagle,  I want to schedule the two other tests at different times...  Or should I just use sleep?  say sleep for 5 seconds...  try again... sleep for 15 minutes try again....   sleep for a day.....  start over....


Thanks
ryno71
Mayank SAssociate Director - Product EngineeringCommented:
Yes it can be done that way. Because it looks like you want to wait for the result of the task first and then decide when the next one is to be scheduled. Or you can schedule the other task inside the run () method of the first task. Mick, correct me if I am wrong.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mick BarryJava DeveloperCommented:
> Is there a way to capture the result of a scheduled task if it fails

Store the results in your TimerTask instance.

> Or you can schedule the other task inside the run () method of the first task.

Yes, scheduling the other task from within your TimerTask can be done
ryno71Author Commented:
Thanks guys

Think I will just use my on thread and sleep.  If I created a timer or a timertask I'd have more than one thread....  Was already told not to use a TimerTask too.

Thanks alot

ryno71
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.