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 5 of 5

Thread: How to control the time that a function takes to execute in a loop?

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to control the time that a function takes to execute in a loop?

    Im doing a program that open up a folder, makes a list of all the files in a folder, then processes each file one by one in a loop.
    the problem is that some of the files are not in the proper format for the parser and it just get stuck at some point because of a bug. I cannot do anything about the parser. But what I wanted to do a code that lets me try to process the file for some time period and if after that its not done. skip it and go to the next file. im not that used to java and dont know where to start. Thank you all in advance


  2. #2
    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: How to control the time that a function takes to execute in a loop?

    I wanted to do a code that lets me try to process the file for some time period and if after that its not done. skip it
    One way would be in the method to save the starting time using: System.currentTimeMillis() and then test the time used at selected points in the method/loop by getting the current time and computing the time used and exiting the code if too much time was used.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to control the time that a function takes to execute in a loop?

    Im using junit to run the test. I added the timeout but when the loops get a file that get stuck when the time limit its over it stop the test and just dont continue to the next file. Any ideas about that??

  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: How to control the time that a function takes to execute in a loop?

    when the time limit its over it stop the test
    No ideas without seeing the code. Here is how I thought it could be done:
    begin outer loop
      get file to process
      save starting time
      begin inner loop
        if (time used up) //  duration = current time - starting time
           break out of inner loop
        do some processing
      end inner loop
      finish up as needed
    end outer loop

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to control the time that a function takes to execute in a loop?

    If I understand the problem, your process is linear - so if at some point a piece of code gets 'stuck', and that is beyond your control (for example a 3rd party library or a Runtime.exec() call), then you should split your process up to run parallel using more than a single thread. For example, you could start a Timer, kickoff the process, and monitor completion of the process using a boolean flag. When the timer fires it checks if if the flag has been set (task completed) or not (incomplete) and acts accordingly. To learn more about threads, see Lesson: Concurrency (The Java™ Tutorials > Essential Classes)

Similar Threads

  1. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 07:37 AM
  2. Could not execute the file
    By miaaa00 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 20th, 2011, 08:28 AM
  3. Code doesn't execute properly
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 12:36 PM
  4. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM
  5. unable to execute prepared statement
    By nrao in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 11th, 2010, 08:26 PM