Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: execute a method over a maximum period of time

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: execute a method over a maximum period of time

    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.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: execute a method over a maximum period of time

    Quote Originally Posted by Norm View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

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

Similar Threads

  1. [SOLVED] .jar file won't execute
    By pajfilms in forum What's Wrong With My Code?
    Replies: 27
    Last Post: August 3rd, 2011, 07:43 PM
  2. How to control the time that a function takes to execute in a loop?
    By GodspeedPR in forum Loops & Control Statements
    Replies: 4
    Last Post: July 20th, 2011, 03:37 PM
  3. Setting JFrame maximum size that pack() can not violate
    By Javabeginner in forum AWT / Java Swing
    Replies: 3
    Last Post: September 3rd, 2010, 05:53 AM
  4. how to execute a simple display without a main method
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 13th, 2010, 07:28 AM
  5. Remove from array after a period of time
    By Marty in forum Collections and Generics
    Replies: 1
    Last Post: September 1st, 2009, 07:57 AM

Tags for this Thread