Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. // Warning: deadlock-prone!
  2. class Taxi {
  3.     @GuardedBy("this") private Point location, destination;
  4.     private final Dispatcher dispatcher;
  5.  
  6.     public Taxi(Dispatcher dispatcher) {
  7.         this.dispatcher = dispatcher;
  8.     }
  9.  
  10.     public synchronized Point getLocation() {
  11.         return location;
  12.     }
  13.  
  14.     public synchronized void setLocation(Point location) {
  15.         this.location = location;
  16.         if (location.equals(destination))
  17.             dispatcher.notifyAvailable(this);
  18.     }
  19. }
  20.  
  21. class Dispatcher {
  22.     @GuardedBy("this") private final Set<Taxi> taxis;
  23.     @GuardedBy("this") private final Set<Taxi> availableTaxis;
  24.  
  25.     public Dispatcher() {
  26.         taxis = new HashSet<Taxi>();
  27.         availableTaxis = new HashSet<Taxi>();
  28.     }
  29.  
  30.     public synchronized void notifyAvailable(Taxi taxi) {
  31.         availableTaxis.add(taxi);
  32.     }
  33.  
  34.     public synchronized Image getImage() {
  35.         Image image = new Image();
  36.         for (Taxi t : taxis)
  37.             image.drawMarker(t.getLocation());
  38.  
  39.         return image;
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement