Skip to content
gqlxj1987's Blog
Go back

Java cpu cache

Edit page

原文链接

private void lock() {
  while (!state.compareAndSet(0, 1))
      //对于未能取得锁所有权的线程,在内层循环上等待
      //因为获取了 state 一份共享的高速缓存副本,
      //不会再进一步产生总线通信量
      while (state.get() == 1)
          ;
}

测试两个版本的 lock 方法,在我的机器上有比较明显的差异,没有内层循环的 lock 版本耗时在 6-8 秒,而增加了内层循环的 lock 耗时在 2.5 -5 秒

关于什么场景下会使用到高速缓存


Edit page
Share this post on:

Previous Post
Catergory theory
Next Post
RL lib intro