用内部成员实例做lock,而不用实例自身this做同步有何好处?

譬如Writer定义就用一个lock,

public abstract class Writer implements Appendable, Closeable, Flushable {

  /**
   * The object used to synchronize operations on this stream.  For
   * efficiency, a character-stream object may use an object other than
   * itself to protect critical sections.  A subclass should therefore use
   * the object in this field rather than <tt>this</tt> or a synchronized
   * method.
   */
  protected Object lock;
  。。。
  }

看了英文解释,说是有利于子类,但是具体好处没说清楚。

阅读 1.7k
2 个回答

哪里提到内部类了???
无参构造器不是赋值this吗?

    protected Writer() {
        this.lock = this;
    }

解耦吧。用户可以传自己的lock 进去

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题