Link to home
Start Free TrialLog in
Avatar of ryno71
ryno71

asked on

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
Avatar of Mayank S
Mayank S
Flag of India image

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 ++ ) ;
}
Avatar of a122178
a122178

System.out.println("Process 1 is done");

Then check out the print out message on the console.
Avatar of ryno71

ASKER

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



}

}
Avatar of ryno71

ASKER

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)



}

}
  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
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.
Avatar of ryno71

ASKER

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

Avatar of ryno71

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ryno71

ASKER

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