【发布时间】:2013-10-12 07:56:42
【问题描述】:
为什么 Sun 不使用 synchronized(this) 而不是 mutex = this 然后使用 synchronized(mutex) ?
我看不出做他们所做的任何好处?我错过了什么吗?
static class SynchronizedCollection<E> implements Collection<E>, Serializable {
private static final long serialVersionUID = 3053995032091335093L;
final Collection<E> c; // Backing Collection
final Object mutex; // Object on which to synchronize
SynchronizedCollection(Collection<E> c) {
if (c==null)
throw new NullPointerException();
this.c = c;
mutex = this;
}
SynchronizedCollection(Collection<E> c, Object mutex) {
this.c = c;
this.mutex = mutex;
}
public int size() {
synchronized (mutex) {return c.size();}
}
public boolean isEmpty() {
synchronized (mutex) {return c.isEmpty();}
}
【问题讨论】:
-
尽量不要问“Sun 为什么要这样做”这样的问题,因为只有 Sun 的开发人员才能回答这个问题。另外,您将如何验证您得到的答案是否正确?因此,这个问题可能没有任何“正确”的答案。
标签: java collections synchronization