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

Thread: Giving Thread.sleep( ); a decimal value

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Giving Thread.sleep( ); a decimal value

    Is this possible in any way? The values are always between 0 and 2, and it is looped over 2000 times (Yes it has to be that way) so accuracy does count. I would post the source but I feel it would not help in this situation. Thread.sleep() won't accept non-integer values, so if anyone knows some sort of workaround, please let me know.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Giving Thread.sleep( ); a decimal value

    Unfortunately, the sleeping mechanism of many operating systems does not have resolution below 1 ms (and many don't even have this). If you need precise timing, the only way I can think of is using some sort of "active" waiting, or resorting to JNI/C++.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Knox (April 9th, 2011)

  4. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Giving Thread.sleep( ); a decimal value

    Thanks for the reply, I tried using C++ but quickly realized this would be too difficult for each time I wanted to test it, so I ended up using a loop to pause the program.

    time1=System.currentTimeMillis();
        while (time2-time1 < sleeptime) {
                time2=System.currentTimeMillis();
                if (time2-time1 == sleeptime || time2-time1 > sleeptime) {break;}
         }

    It still is not perfectly accurate (and nothing will ever be), but it is as good as I need it to be.
    Last edited by Knox; April 9th, 2011 at 09:48 PM.

Similar Threads

  1. JavaCompilerobject giving an error WITHOUT ide
    By divs1210 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 08:49 PM
  2. [SOLVED] Thread.sleep() in while loop not working
    By mds1256 in forum Threads
    Replies: 4
    Last Post: January 13th, 2011, 06:02 AM
  3. SAAJ giving exception with large attachments
    By Veronica in forum Web Frameworks
    Replies: 2
    Last Post: January 4th, 2011, 04:13 AM
  4. Vector Addition with small numbers giving NaN
    By Zula in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 28th, 2010, 09:29 PM
  5. Thread Sleep, Timer, Button Question
    By tabutcher in forum Java Theory & Questions
    Replies: 1
    Last Post: May 1st, 2010, 02:54 AM