• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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
0
ryno71
Asked:
ryno71
  • 5
  • 4
  • 2
  • +1
2 Solutions
 
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 ++ ) ;
}
0
 
a122178Commented:
System.out.println("Process 1 is done");

Then check out the print out message on the console.
0
 
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



}

}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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)



}

}
0
 
objectsCommented:
  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
0
 
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.
0
 
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
0
 
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.

0
 
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
0
 
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.
0
 
objectsCommented:
> 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
0
 
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now