Martina312

ОС - Транспорт на криминалци

Apr 23rd, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.94 KB | None | 0 0
  1. //reshenie na:
  2. //https://github.com/finki-mk/OS/tree/master/synchronization/src/main/java/mk/ukim/finki/os/synchronization/exam19/s1
  3.  
  4. package ispiti.criminalTransport;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.HashSet;
  8. import java.util.List;
  9. import java.util.concurrent.Semaphore;
  10.  
  11. class Locks{
  12.     static int numCriminals = 0;
  13.     static int numPoliceman = 0;
  14.     static Semaphore lock;
  15.     static Semaphore criminalEnter;
  16.     static Semaphore policemanEnter;
  17.     static Semaphore doneEntering;
  18.     static Semaphore canExit;
  19.     static Semaphore exitDone;
  20.  
  21. }
  22. class Policeman extends Thread{
  23.  
  24.     public void execute() throws InterruptedException {
  25.         Locks.lock.acquire();
  26.         Locks.numPoliceman++;
  27.         boolean controller = false;
  28.  
  29.         if(Locks.numPoliceman == 2 && Locks.numCriminals>=2){
  30.             controller = true;
  31.             Locks.criminalEnter.release(2);
  32.             Locks.policemanEnter.release(2);
  33.             Locks.numCriminals-=2;
  34.             Locks.numPoliceman-=2;
  35.         }else if(Locks.numPoliceman==4){
  36.             controller = true;
  37.             Locks.policemanEnter.release(4);
  38.             Locks.numPoliceman-=4;
  39.         }else{
  40.             Locks.lock.release();
  41.         }
  42.  
  43.         Locks.policemanEnter.acquire();
  44.         System.out.println("Policeman enters in the car");
  45.         Locks.doneEntering.release();
  46.  
  47.         if(controller){
  48.             Locks.doneEntering.acquire(4);
  49.             System.out.println("Policeman says start driving.");
  50.             Thread.sleep(100);
  51.             System.out.println("Policeman says arrived.");
  52.             Locks.canExit.release(3);
  53.             System.out.println("Policeman exits from the car");
  54.             Locks.exitDone.acquire(3);
  55.             System.out.println("Gotovo");
  56.             Locks.lock.release();
  57.         }else{
  58.             Thread.sleep(100);
  59.             Locks.canExit.acquire();
  60.             System.out.println("Policeman exits from the car");
  61.             Locks.exitDone.release();
  62.         }
  63.     }
  64.  
  65.     @Override
  66.     public void run() {
  67.         try {
  68.             execute();
  69.         } catch (InterruptedException e) {
  70.             e.printStackTrace();
  71.         }
  72.     }
  73. }
  74.  
  75. class Criminal extends Thread {
  76.  
  77.     public void execute() throws InterruptedException {
  78.         Locks.lock.acquire();
  79.         Locks.numCriminals++;
  80.         Locks.lock.release();
  81.  
  82.         Locks.criminalEnter.acquire();
  83.         System.out.println("Criminal enters in the car");
  84.         Locks.doneEntering.release();
  85.  
  86.         //sega se vozat
  87.         Thread.sleep(100);
  88.         Locks.canExit.acquire();
  89.         System.out.println("Criminal exits from the car");
  90.         Locks.exitDone.release();
  91.  
  92.     }
  93.  
  94.     @Override
  95.     public void run() {
  96.         try {
  97.             execute();
  98.         } catch (InterruptedException e) {
  99.             e.printStackTrace();
  100.         }
  101.     }
  102. }
  103.  
  104. public class CriminalTransport {
  105.  
  106.     public static void init(){
  107.         Locks.lock = new Semaphore(1);
  108.         Locks.policemanEnter = new Semaphore(0);
  109.         Locks.criminalEnter = new Semaphore(0);
  110.         Locks.doneEntering = new Semaphore(0);
  111.         Locks.canExit = new Semaphore(0);
  112.         Locks.exitDone = new Semaphore(0);
  113.     }
  114.  
  115.  
  116.     public static void main(String[] args) throws InterruptedException {
  117.         HashSet<Thread> threads = new HashSet<Thread>();
  118.         for (int i = 0; i < 60; i++) {
  119.             Policeman red = new Policeman();
  120.             threads.add(red);
  121.             Criminal green = new Criminal();
  122.             threads.add(green);
  123.         }
  124.         init();
  125.         // run all threads in background
  126.         for(Thread t:threads){
  127.             t.start();
  128.         }
  129.         // after all of them are started, wait each of them to finish for maximum 1_000 ms
  130.         for(Thread t:threads){
  131.             t.join();
  132.         }
  133.         // for each thread, terminate it if it is not finished
  134.         System.out.println("Uspeshno!!!");
  135.     }
  136.  
  137. }
Add Comment
Please, Sign In to add comment