Thread.sleep waits more than expected

后端 未结 4 1813
南笙
南笙 2021-01-17 20:14

The following code:

long msBefore = System.currentTimeMillis();
//Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
try
{Thread.sleep(200);
} catch (I         


        
4条回答
  •  渐次进展
    2021-01-17 21:07

    You're not taking into account the time it spends processing.

        try {
            long processingStart = System.currentTimeMillis();
    
            long processingFinish = System.currentTimeMillis();
            long processTime = 600 - (processingFinish - processingStart);
            Thread.sleep(processTime);
    
        } catch (InterruptedException ex) {
    
        }
    

提交回复
热议问题