Taking a Very Short Nap: Sub-Millisecond Sleep for Threads

As we discussed in the previous section, Java 5 added a nanoTime method to the System class to ensure that time measurements can keep up with faster systems. Even in earlier versions of Java, threads can have sleep times of less than a millisecond. You might recall that Java's threading mechanism has a sleep method that takes an int parameter representing the sleep time in milliseconds. There is also a sleep method that accepts the millisecond parameter, plus an additional time in nanoseconds. If you set the millisecond time to 0, then the thread will sleep for the specified number of ...

Get Wicked Cool Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.