stillflame
Programmer
- Jan 12, 2001
- 416
hi all,
here's the basic problem i'm trying to solve.
i have a method that can take a really long time depending on the data, and i need to know which of the data in my data set takes more than, say 2 minutes. also, some data may take MANY MANY hours, so i don't want to have to sit through those with this program, thus i want to be able to stop the execution of the method after two minutes.
my idea was this:
i made a little class, i called it TimeOut, which extended TimerTask. in the [tt]run()[/tt] method of TimeOut, all it does is throw an exception. maybe i should be using an ActionListener or something along those lines for this, i'm not really sure.
then, i have a loop over my data set, invoking the method with each piece. it looks something like this:[tt]
//inside the loop...
try {
long delay = 1000 * 60 * 2; //2 minutes of millisecs
timer = new Timer();
timer.schedule(new TimeOut(), delay);
method.invoke(some_object, data_set.getData(i));
timer.cancel();
}
catch (TimeOutException e) {
//do some logging and stuff.
}
//end of loop.[/tt]
i thought this would work, but weird things happen: for any invocation that takes only a few milliseconds, the loop goes through fine. however, on data that takes longer than that, the program stops doing anything, processor activity drops to none, and memory usage for the program increases by 15 megs... even after 2 minutes is up and the timer should be sending out an exception, nothing happens.
i have no idea why this is screwing up like it is. without the timer, the method just executes, no matter how long it takes.
if someone can tell me what i'm doing wrong, or better yet, show me a much simpler and easy way to do what i'm trying to do, i would be very appreciative. BTW, i searched through the archives here for timeouts and uses of the Timer class, but couldn't find anything that fit my situation. i apologize if i did miss something in my search.
also, the idea strikes me as i re-read this post, that maybe i have to implement this in a multi-threaded way. i was assuming (read hoping) that Timer would automatically create itself a little thread just for its own use so that the execution of my method wouldn't interfere with it, but i don't have any documentation to support this. hmm...
thanks in advance for any help given,
stillflame "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."
here's the basic problem i'm trying to solve.
i have a method that can take a really long time depending on the data, and i need to know which of the data in my data set takes more than, say 2 minutes. also, some data may take MANY MANY hours, so i don't want to have to sit through those with this program, thus i want to be able to stop the execution of the method after two minutes.
my idea was this:
i made a little class, i called it TimeOut, which extended TimerTask. in the [tt]run()[/tt] method of TimeOut, all it does is throw an exception. maybe i should be using an ActionListener or something along those lines for this, i'm not really sure.
then, i have a loop over my data set, invoking the method with each piece. it looks something like this:[tt]
//inside the loop...
try {
long delay = 1000 * 60 * 2; //2 minutes of millisecs
timer = new Timer();
timer.schedule(new TimeOut(), delay);
method.invoke(some_object, data_set.getData(i));
timer.cancel();
}
catch (TimeOutException e) {
//do some logging and stuff.
}
//end of loop.[/tt]
i thought this would work, but weird things happen: for any invocation that takes only a few milliseconds, the loop goes through fine. however, on data that takes longer than that, the program stops doing anything, processor activity drops to none, and memory usage for the program increases by 15 megs... even after 2 minutes is up and the timer should be sending out an exception, nothing happens.
i have no idea why this is screwing up like it is. without the timer, the method just executes, no matter how long it takes.
if someone can tell me what i'm doing wrong, or better yet, show me a much simpler and easy way to do what i'm trying to do, i would be very appreciative. BTW, i searched through the archives here for timeouts and uses of the Timer class, but couldn't find anything that fit my situation. i apologize if i did miss something in my search.
also, the idea strikes me as i re-read this post, that maybe i have to implement this in a multi-threaded way. i was assuming (read hoping) that Timer would automatically create itself a little thread just for its own use so that the execution of my method wouldn't interfere with it, but i don't have any documentation to support this. hmm...
thanks in advance for any help given,
stillflame "If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."