Tag: 纳米

将Java虚拟机与System.nanoTime同步

将java虚拟机与System.nanoTime()同步是否有意义? 我的意思是 : 调用System.nanoTime()并将结果放入t1 A向B发送数据包 当在B上接收到来自A的数据包时,B调用System.nanoTime()并将结果发送给A. 当在A上接收到来自B的数据包时,A调用System.nanoTime()并将结果放入t3,将来自B的数据包中接收的时间放在t2中 timeshift =(t3-t1)/ 2-t2 使用System.nanoTime来做到这一点是正确的吗? 谢谢 !

如何通过减去2个nanoTime对象获得有意义的结果?

我创建了一个监视请求长度的filter。 long start = System.nanoTime(); … long end = System.nanoTime(); 我怎么能从这里得到毫秒数?

调用Thread.sleep()后,System.nanoTime()测量经过的时间的准确度会降低

我在这里遇到一个非常不寻常的问题。 似乎调用Thread.sleep(n),其中n> 0将导致以下System.nanoTime()调用不太可预测。 下面的代码演示了这个问题。 在我的计算机上运行它(rMBP 15“2015,OS X 10.11,jre 1.8.0_40-b26)输出以下结果: Control: 48497 Random: 36719 Thread.sleep(0): 48044 Thread.sleep(1): 832271 在运行Windows 8的虚拟机上(VMware Horizo​​n,Windows 8.1,为1.8.0_60-b27): Control: 98974 Random: 61019 Thread.sleep(0): 115623 Thread.sleep(1): 282451 但是,在企业服务器上运行它(VMware,RHEL 6.7,jre 1.6.0_45-b06): Control: 1385670 Random: 1202695 Thread.sleep(0): 1393994 Thread.sleep(1): 1413220 令人惊讶的是我期望的结果。 很明显,Thread.sleep(1)会影响下面代码的计算。 我不知道为什么会这样。 有人有线索吗? 谢谢! public class Main { public static void main(String[] args) { […]

为什么我的System.nanoTime()坏了?

我自己和我这个时代的另一位开发人员最近从工作中的Core 2 Duo机器搬到了新的Core 2 Quad 9505; 两者都使用JDK 1.6.0_18运行Windows XP SP3 32位。 在这样做的时候,我们对一些时序/统计/度量聚合代码的自动unit testing很快就会失败,因为看起来似乎是从System.nanoTime()返回的荒谬值。 在我的机器上可靠地显示此行为的测试代码是: import static org.junit.Assert.assertThat; import org.hamcrest.Matchers; import org.junit.Test; public class NanoTest { @Test public void testNanoTime() throws InterruptedException { final long sleepMillis = 5000; long nanosBefore = System.nanoTime(); long millisBefore = System.currentTimeMillis(); Thread.sleep(sleepMillis); long nanosTaken = System.nanoTime() – nanosBefore; long millisTaken = […]

如何暂停java线程一小段时间,如100纳秒?

我知道Thread.sleep()可以让java线程暂停一段时间,比如某些毫秒和某个纳秒。 但问题是这个函数的调用也会导致开销。 例如,如果我想要一个线程暂停100纳秒,我调用Thread.sleep(0,100) 。 这个过程的全部成本是invocation_cost + 100 nanosceonds ,这可能比我想要的要大得多。 我怎么能避免这个问题,实现我的目的呢? 我需要这个的原因是我想离线进行模拟。 我描述了任务的执行时间; 现在我想通过在同一时间段内挂起一个线程来模拟这个执行时间。 谢谢!