Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3.  
  4.  
  5. public class ParkingLot{
  6.  
  7.     private Map<Pair<Integer>,Date> park;
  8.     private LinkedList<Pair<Integer>> occupPark;
  9.     private int i;
  10.     private int j;
  11.     private int m;
  12.     private int n;
  13.     private int maxCars;
  14.    
  15.     public ParkingLot (int n,int m){
  16.         this.n=n;
  17.         this.m=m;
  18.         maxCars=n*m;
  19.         park = new HashMap<>();
  20.         occupPark = new LinkedList<>();
  21.  
  22.  
  23.     }
  24.     public synchronized Pair<Integer> carIn(){
  25.        
  26.         Pair<Integer> pos;
  27.         //Parcheggio Pieno
  28.         if(maxCars<=0) return null;
  29.         // Ci sono posti liberi "non continui"
  30.         if (occupPark.size()>0){
  31.  
  32.             pos = occupPark.removeFirst();
  33.             park.put(pos,new Date());
  34.             maxCars--;
  35.             return pos;
  36.         }
  37.         pos = new Pair<Integer> (i,j);
  38.         park.put(pos,new Date());
  39.         maxCars--;
  40.  
  41.         if(j==m && i!=n){
  42.             j=0;
  43.             i++;
  44.         }
  45.         else j++;
  46.         return pos;
  47.  
  48.     }
  49.  
  50.     public int getMax(){return maxCars;}
  51.  
  52.     public synchronized Integer carOut(Pair pos){
  53.    
  54.         Date currDate = new Date();
  55.         Set<Pair<Integer>> set = park.keySet();
  56.         if(!set.contains(pos)) return null;
  57.         Date carDate = park.get(pos);
  58.         set.remove(pos);
  59.         occupPark.add(pos);
  60.         maxCars++;
  61.         return currDate.getSeconds() - carDate.getSeconds();
  62.  
  63.    }
  64.    
  65.     public static void main(String[] args){
  66.          
  67.        final ParkingLot p =new ParkingLot(10,10);
  68.         BlockingQueue<Pair<Integer>> cars = new LinkedBlockingQueue<>();
  69.  
  70.         Thread abusivo = new Thread(){
  71.  
  72.             @Override
  73.             public void run(){
  74.                 Pair<Integer> pos;
  75.                 synchronized(cars){
  76.                     // Ci sono ancora posti auto
  77.                     while(p.getMax()>0){
  78.                             try{
  79.                                 cars.wait(1000);
  80.                                 cars.add(p.carIn());
  81.                                 cars.notifyAll();
  82.                             }catch(InterruptedException e){}
  83.  
  84.                     }
  85.                    
  86.                     cars.notifyAll();
  87.                 }
  88.             }
  89.          };
  90.  
  91.         Thread finanza = new Thread(){
  92.             @Override
  93.             public void run(){
  94.                 synchronized(cars){
  95.                     while(cars.size()>0 ){
  96.                    try{
  97.                    
  98.                          cars.wait(1000);
  99.                          System.out.println(p.carOut(cars.take()));
  100.                          cars.notifyAll();
  101.                          
  102.                      }
  103.                  
  104.                  catch(InterruptedException e){}
  105.                 }
  106.             }
  107.             cars.notifyAll();
  108.  
  109.             }
  110.          };
  111.  
  112.         try{
  113.         abusivo.start();
  114.         finanza.start();
  115.         abusivo.join();
  116.         finanza.join();
  117.         }
  118.         catch(InterruptedException e){}
  119.                    
  120.      
  121. }
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement