execute a method over a maximum period of time
Good Afternoon in my time zone.
I am using the JavaMail api , and theres is a method in the Folder class called "search" that sometimes take too long to execute. What i want is to execute this method over a maximum period of time( say for example 15 seconds in maximum) , that way i am sure that this method will not run up more than 15 seconds.
Pseudo Code
messages = maximumMethod(Folder.search(),15);
Do i have to create a thread just to execute this methos and in the main thead use the wait method ?
Thanks Very Much
Re: execute a method over a maximum period of time
Yep, sounds like you should use a Thread (or a SwingWorker), and if the time has expired (or the user cancels or something) before the method completes, just ignore the results.
Re: execute a method over a maximum period of time
Also, the SwingWorker API lists a cancel() method that you might want to look into. I'm not very familiar with SwingWorkers though.
Re: execute a method over a maximum period of time
Quote:
What i want is to execute this method over a maximum period of time
Or you could have the method take care of how long it runs. It could capture the start time and get and test the current time as it executes and return when it has run long enough.
See the System currentTimeMillis method for getting the current time.
Re: execute a method over a maximum period of time
Quote:
Originally Posted by
Norm
Or you could have the method take care of how long it runs. It could capture the start time and get and test the current time as it executes and return when it has run long enough.
See the System currentTimeMillis method for getting the current time.
That would be the perfect solution for a method that he has access to, but it sounds like he's using a "black box" method (or at least one that he can't change) in the JavaMail API.
Re: execute a method over a maximum period of time
I think the answer to your question is 'no'. You could come up with a technique using a separate thread that returned after 15 seconds, but the long-running method would still be running in the separate thread. It might look like you made a 15-second call the first time you tried it, but it wouldn't allow you to make 4 such calls per minute, if the previous calls were still running and the subsequent calls were equally long-running.
Folder (Java EE 5 SDK)
Google's gmail web client does fast server-side searching, but for the rest of us client-side searching (with the attendant colossal cache - used Thunderbird continuously for a few years?) is probably the only solution.