主要是缩小了锁的粒度. pls, 原子变量比对象锁效率好一点.
> 在 2018年6月10日,下午3:25,卢松 <lusong19860...@gmail.com> 写道: > > 各位大佬好。 > 有个疑问,apache版本ServiceThread是这么实现的: > > protected final CountDownLatch2 waitPoint = new CountDownLatch2(1); > > protected void waitForRunning(long interval) { > if (hasNotified.compareAndSet(true, false)) { > this.onWaitEnd(); > return; > } > > //entry to wait > waitPoint.reset(); > > try { > waitPoint.await(interval, TimeUnit.MILLISECONDS); > } catch (InterruptedException e) { > log.error("Interrupted", e); > } finally { > hasNotified.set(false); > this.onWaitEnd(); > } > } > > 而很早以前的alibaba版本是这么实现的, > > > protected void waitForRunning(long interval) { > synchronized (this) { > if (this.hasNotified) { > this.hasNotified = false; > this.onWaitEnd(); > return; > } > > try { > this.wait(interval); > } catch (InterruptedException e) { > LOGGER.error(e.getMessage(), e); > } finally { > this.hasNotified = false; > this.onWaitEnd(); > } > } > } > > 请问,这两种实现的主要区别是什么?有什么特殊要考虑的吗? > 有wiki说明这个修改吗? > > 谢谢! > > > 卢松 > best regards