你好,zhanhui,

我说的不同不是AtomicBoolean那块,如下:

apache版本使用的是这个对象 CountDownLatch2 waitPoint = new CountDownLatch2(1);
做的wait操作。

而alibaba版本使用的object的wait操作,

这两种实现有什么区别?
object.wait会导致死锁吗?或者不会从睡眠中醒来?


Thanks
卢松


Zhanhui Li <[email protected]> 于2018年6月11日周一 上午10:04写道:
>
> 主要是缩小了锁的粒度. pls, 原子变量比对象锁效率好一点.
>
> > 在 2018年6月10日,下午3:25,卢松 <[email protected]> 写道:
> >
> > 各位大佬好。
> > 有个疑问,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
>

Reply via email to