Results 1 to 4 of 4
  1. #1
    Vinn is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Measure limited time for a process

    Hi

    I want to run a process in a thread during a limited time (say, 1000 nanoseconds). I created some method to measure the time. After the time is gone, a boolean variable turns into "false", and the thread should stop. Now, it does not seem to work, and I am not sure where to search for the error.

    My question: should the following code turn the value of "busy" into "false" after 1000 nanoseconds, or not?

    Java Code:
    long limitedTime = 1000;
    boolean busy;
    
    public void processing() {
      long startTime = System.nanoTime();
      long duration = System.nanoTime() - startTime;
    
      if (duration < limitedTime) {
        busy = true;
        }
    
      if (duration => limitedTime( {
        busy = false;
        }
      }
    Thanks already for your answer!
    Last edited by Vinn; 03-06-2012 at 08:17 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Measure limited time for a process

    You never update the duration. So your first while loop will never exit. Add some print statements, or better yet step through it with a debugger, to understand exactly what's going on.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Vinn is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Measure limited time for a process

    Thanks Kevin

    I changed the "while" into "if", but... that doesn't seem to make much sense either. Maybe indeed time to explore the debugger...

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Measure limited time for a process

    Your only problem is that you only set the duration once. It seems to me that you should be recalculating that each time.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 08-06-2011, 02:17 AM
  2. Replies: 1
    Last Post: 11-09-2010, 12:42 PM
  3. Need help with Process execution time display
    By murali18 in forum New To Java
    Replies: 5
    Last Post: 03-18-2010, 11:19 PM
  4. How can I measure the data rate of my connection
    By islamfunny in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-25-2008, 03:27 PM
  5. How can I set a time out on the loading process
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 04:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •