鍍金池/ 問答/Java  Linux/ 用內(nèi)部成員實例做lock,而不用實例自身this做同步有何好處?

用內(nèi)部成員實例做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;
  。。。
  }

看了英文解釋,說是有利于子類,但是具體好處沒說清楚。

回答
編輯回答
笑浮塵

哪里提到內(nèi)部類了???
無參構(gòu)造器不是賦值this嗎?

    protected Writer() {
        this.lock = this;
    }
2018年1月31日 12:26
編輯回答
黑與白

解耦吧。用戶可以傳自己的lock 進去

2018年3月20日 08:26