P
puzzlecracker
This example is taken from Java Concurrency in Practice. I am not sure
I see the deadlock. I also have a few follow up questions about the
classes commented
class Taxi
{
@GuardedBy("this") private Point location, destination; // what
is the story with @GuardedBy("this")
private final Dispatcher dispatcher; //does final here mean that
it can only be assigned once in a constructor only?
public Taxy (Dispatcher dispatcher)
{
this.dispatcher=dispatcher;
}
public synchronized Point GetLocation(){ return location;}
public synchronized void setLocation(Point location)
{
this.location=location;
if(location.equals(destination))
despatcher.notifyAvailable(this);
}
}
class Dispatcher
{
@GuardedBy("this") private final Set<Taxi> taxis;
@GuardedBy("this") private final Set<Taxi> availableTaxis;
public Dispatcher()
{
taxis=new HashSet<Taxi>();
availableTaxis=new HashSet<Taxi>();
}
public synchronize void notifyAvailable(Taxi taxi)
{
avaliableTaxis.add(taxi);
}
public synchronize Image GetImage()
{
Image image=new Image();
for(Taxi t:taxis)
image.drawMaker(t.getLocation);
return Image;
}
}
Ask me if anything needs a clarification.
Thanks.
I see the deadlock. I also have a few follow up questions about the
classes commented
class Taxi
{
@GuardedBy("this") private Point location, destination; // what
is the story with @GuardedBy("this")
private final Dispatcher dispatcher; //does final here mean that
it can only be assigned once in a constructor only?
public Taxy (Dispatcher dispatcher)
{
this.dispatcher=dispatcher;
}
public synchronized Point GetLocation(){ return location;}
public synchronized void setLocation(Point location)
{
this.location=location;
if(location.equals(destination))
despatcher.notifyAvailable(this);
}
}
class Dispatcher
{
@GuardedBy("this") private final Set<Taxi> taxis;
@GuardedBy("this") private final Set<Taxi> availableTaxis;
public Dispatcher()
{
taxis=new HashSet<Taxi>();
availableTaxis=new HashSet<Taxi>();
}
public synchronize void notifyAvailable(Taxi taxi)
{
avaliableTaxis.add(taxi);
}
public synchronize Image GetImage()
{
Image image=new Image();
for(Taxi t:taxis)
image.drawMaker(t.getLocation);
return Image;
}
}
Ask me if anything needs a clarification.
Thanks.