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

前端 未结 6 1138
后悔当初
后悔当初 2020-12-09 02:09

I know Thread.sleep() can make a java thread suspend for a while, like certain milliseconds and certain nanoseconds. But the problem is the invocation of th

6条回答
  •  不知归路
    2020-12-09 02:36

    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: I suspect you'll get best results on jrockit + solaris. The numbers on a windows box are terrible.

    @Test
    public void testWait(){
        final long INTERVAL = 100;
        long start = System.nanoTime();
        long end=0;
        do{
            end = System.nanoTime();
        }while(start + INTERVAL >= end);
        System.out.println(end - start);
    }
    

提交回复
热议问题