Cur*_*ous 0 java multithreading synchronized-block
以下是在讨论开放调用时来自实践本书中java并发的代码片段.我没有得到的是setLocation方法的声明方式,它已经被同步并再次在同一个方法中调用synchronized(this)块,为什么呢?是类型错误吗?synchronized方法已经锁定了这个方法然后为什么再次对同一个对象?
@ThreadSafe
class Taxi {
@GuardedBy("this") private Point location, destination;
private final Dispatcher dispatcher;
...
public synchronized Point getLocation() {
return location;
}
public synchronized void setLocation(Point location) {
boolean reachedDestination;
synchronized (this) {
this.location = location;
reachedDestination = location.equals(destination);
}
if (reachedDestination)
dispatcher.notifyAvailable(this);
}
}
Run Code Online (Sandbox Code Playgroud)