How to suspend a java thread for a small period of time, like 100 nanoseconds?

The granularity of sleeps is generally bound by the thread scheduler’s interrupt period. In Linux, this interrupt period is generally 1ms in recent kernels. In Windows, the scheduler’s interrupt period is normally around 10 or 15 milliseconds If I have to halt threads for periods less than this, I normally use a busy wait EDIT: … Read more

How to convert epoch time with nanoseconds to human-readable?

First, convert it to a datetime object with second precision (floored, not rounded): >>> from datetime import datetime >>> dt = datetime.fromtimestamp(1360287003083988472 // 1000000000) >>> dt datetime.datetime(2013, 2, 7, 17, 30, 3) Then to make it human-readable, use the strftime() method on the object you get back: >>> s = dt.strftime(‘%Y-%m-%d %H:%M:%S’) >>> s ‘2013-02-07 … Read more

Precision vs. accuracy of System.nanoTime()

In Clojure command line, I get: user=> (- (System/nanoTime) (System/nanoTime)) 0 user=> (- (System/nanoTime) (System/nanoTime)) 0 user=> (- (System/nanoTime) (System/nanoTime)) -641 user=> (- (System/nanoTime) (System/nanoTime)) 0 user=> (- (System/nanoTime) (System/nanoTime)) -642 user=> (- (System/nanoTime) (System/nanoTime)) -641 user=> (- (System/nanoTime) (System/nanoTime)) -641 So essentially, nanoTime doesn’t get updated every nanosecond, contrary to what one might intuitively … Read more

Java 8 Instant.now() with nanosecond resolution?

While default Java8 clock does not provide nanoseconds resolution, you can combine it with Java ability to measure time differences with nanoseconds resolution, thus creating an actual nanosecond-capable clock. public class NanoClock extends Clock { private final Clock clock; private final long initialNanos; private final Instant initialInstant; public NanoClock() { this(Clock.systemUTC()); } public NanoClock(final Clock … Read more

Is System.nanoTime() completely useless?

This answer was written in 2011 from the point of view of what the Sun JDK of the time running on operating systems of the time actually did. That was a long time ago! leventov’s answer offers a more up-to-date perspective. That post is wrong, and nanoTime is safe. There’s a comment on the post … Read more