prometheus800

ОС Лаб 3: „Танц со студентите“ ┏(・o・)┛♪┗ (・o・) ┓

Mar 30th, 2021
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.37 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.List;
  4. import java.util.Random;
  5. import java.util.concurrent.Semaphore;
  6.  
  7. public class TancSoStudentite {
  8.     //TODO: Definicija na globalni promenlivi i semafori
  9.     static Semaphore male;
  10.     static Semaphore female;
  11.     static Semaphore maleHere;
  12.     static Semaphore femaleHere;
  13.     static Semaphore ready;
  14.     static Semaphore dance;
  15.  
  16.  
  17.     public void init() {
  18.         //TODO: da se implementira
  19.         male = new Semaphore(10);
  20.         female = new Semaphore(10);
  21.         maleHere = new Semaphore(0);
  22.         femaleHere = new Semaphore(0);
  23.         ready = new Semaphore(0);
  24.         dance = new Semaphore(3);
  25.     }
  26.  
  27.     class Masko extends Thread {
  28.         //TODO: Definicija  na promenlivi za sostojbata
  29.  
  30.         public void ucestvo() throws InterruptedException {
  31.             //TODO: da se implementira
  32.             male.acquire(); // Get a male
  33.             show.presobleci(); // Change clothes n stuff
  34.             maleHere.release(); // Send signal to female
  35.             femaleHere.acquire(); // Wait for a female to change
  36.  
  37.             ready.release(); //Signal that a dance can be executed
  38.  
  39.             // samo maskoto go povikuva metodot tancuvaj
  40.             dance.acquire(); // Acquire a permit to execute dance
  41.             show.tancuvaj(); // Showtime ┏(・o・)┛♪┗ (・o・) ┓
  42.             dance.release(); // Release so that another pair could dance
  43.             male.release();  // One male exits the changing room, tell the one waiting to enter
  44.         }
  45.  
  46.         @Override
  47.         public void run() {
  48.             try {
  49.                 ucestvo();
  50.             } catch (InterruptedException e) {
  51.                 // Do nothing
  52.             } catch (Exception e) {
  53.                 exception = e;
  54.                 hasException = true;
  55.             }
  56.         }
  57.  
  58.         @Override
  59.         public String toString() {
  60.             return String.format("m\t%d", getId());
  61.         }
  62.         public Exception exception = null;
  63.     }
  64.  
  65.     class Zensko extends Thread {
  66.         //TODO: Definicija  na promenlivi za sostojbata
  67.  
  68.         public void ucestvo() throws InterruptedException {
  69.             //TODO: da se implementira
  70.             female.acquire();
  71.             show.presobleci();
  72.             femaleHere.release();
  73.             maleHere.acquire();
  74.             ready.acquire();
  75.  
  76.             female.release();
  77.         }
  78.  
  79.         @Override
  80.         public void run() {
  81.             try {
  82.                 ucestvo();
  83.             } catch (InterruptedException e) {
  84.                 // Do nothing
  85.             } catch (Exception e) {
  86.                 exception = e;
  87.                 hasException = true;
  88.             }
  89.         }
  90.  
  91.         @Override
  92.         public String toString() {
  93.             return String.format("z\t%d", getId());
  94.         }
  95.         public Exception exception = null;
  96.     }
  97.  
  98.     public static void main(String[] args) {
  99.         try {
  100.             TancSoStudentite environment = new TancSoStudentite();
  101.             environment.start();
  102.         } catch (Exception ex) {
  103.             ex.printStackTrace();
  104.         }
  105.     }
  106.  
  107.     public void start() throws Exception {
  108.         show = new Show();
  109.         init();
  110.         HashSet<Thread> threads = new HashSet<Thread>();
  111.         for (int i = 0; i < BROJ_INSTANCI; i++) {
  112.             Zensko z = new Zensko();
  113.             Masko m = new Masko();
  114.             threads.add(z);
  115.             threads.add(m);
  116.         }
  117.  
  118.         for (Thread t : threads) {
  119.             t.start();
  120.         }
  121.  
  122.         boolean valid = true;
  123.         for (Thread t : threads) {
  124.             if (!hasException) {
  125.                 t.join();
  126.             } else {
  127.                 t.interrupt();
  128.             }
  129.         }
  130.         show.printStatus();
  131.  
  132.     }
  133.  
  134.     public class Show {
  135.  
  136.         public static final int BROJ_GARDEROBA = 10;
  137.         public static final int BROJ_TEREN = 3;
  138.         public static final int TYPE_MASKO = 1;
  139.         public static final int TYPE_ZENSKO = 2;
  140.         public static final int TYPE_UNKNOWN = -1;
  141.  
  142.         public Show() {
  143.         }
  144.         public int brojMaskiGarderoba = 0;
  145.         public int brojZenskiGarderoba = 0;
  146.         public int brojTancuvanja = 0;
  147.         public int maxMaskiGarderoba = 0;
  148.         public int maxZenskiGarderoba = 0;
  149.         public int maxTancuvanja = 0;
  150.  
  151.         public void presobleci() throws RuntimeException {
  152.             log(null, "presobleci start");
  153.             Thread t = Thread.currentThread();
  154.             if (t instanceof Masko) {
  155.                 synchronized (RANDOM) {
  156.                     brojMaskiGarderoba++;
  157.                     if (brojMaskiGarderoba > 10) {
  158.                         exception("Ne moze da ima poveke od 10 maski vo maskata garderoba.");
  159.                     }
  160.                     if (brojMaskiGarderoba > maxMaskiGarderoba) {
  161.                         maxMaskiGarderoba = brojMaskiGarderoba;
  162.                     }
  163.                 }
  164.                 waitRandom();
  165.                 synchronized (RANDOM) {
  166.                     brojMaskiGarderoba--;
  167.                 }
  168.             } else {
  169.                 synchronized (RANDOM) {
  170.                     brojZenskiGarderoba++;
  171.                     if (brojZenskiGarderoba > 10) {
  172.                         exception("Ne moze da ima poveke od 10 zenski vo zenskata garderoba.");
  173.                     }
  174.                     if (brojZenskiGarderoba > maxZenskiGarderoba) {
  175.                         maxZenskiGarderoba = brojZenskiGarderoba;
  176.                     }
  177.                 }
  178.                 waitRandom();
  179.                 synchronized (RANDOM) {
  180.                     brojZenskiGarderoba--;
  181.                 }
  182.             }
  183.             log(null, "presobleci kraj");
  184.         }
  185.  
  186.         public void tancuvaj() throws RuntimeException {
  187.             log(null, "tancuvaj start");
  188.             synchronized (RANDOM) {
  189.                 brojTancuvanja++;
  190.                 if (brojTancuvanja > BROJ_TEREN) {
  191.                     exception("Ne moze paralelno da tancuvaat poveke od 3 para.");
  192.                 }
  193.  
  194.                 if (brojTancuvanja > maxTancuvanja) {
  195.                     maxTancuvanja = brojTancuvanja;
  196.                 }
  197.             }
  198.             waitRandom();
  199.             synchronized (RANDOM) {
  200.                 brojTancuvanja--;
  201.             }
  202.             log(null, "tancuvaj kraj");
  203.         }
  204.  
  205.         private void waitRandom() {
  206.             try {
  207.                 int r;
  208.                 synchronized (RANDOM) {
  209.                     r = RANDOM.nextInt(RANDOM_RANGE);
  210.                 }
  211.                 Thread.sleep(r);
  212.             } catch (Exception e) {
  213.                 //do nothing
  214.             }
  215.         }
  216.  
  217.         private void exception(String message) {
  218.             RuntimeException e = new RuntimeException(message);
  219.             log(e, null);
  220.             hasError = true;
  221.             throw e;
  222.         }
  223.  
  224.         public int getType() {
  225.             Thread t = Thread.currentThread();
  226.             if (t instanceof Masko) {
  227.                 return TYPE_MASKO;
  228.             } else if (t instanceof Zensko) {
  229.                 return TYPE_ZENSKO;
  230.             } else {
  231.                 return TYPE_UNKNOWN;
  232.             }
  233.         }
  234.  
  235.         private synchronized void log(RuntimeException e, String action) {
  236.             Thread t = Thread.currentThread();
  237.             if (e == null) {
  238.                 actions.add(t.toString() + "\t(a): " + action);
  239.             } else {
  240.                 actions.add(t.toString() + "\t(e): " + e.getMessage());
  241.             }
  242.         }
  243.  
  244.         public synchronized void printLog() {
  245.             System.out.println("Poradi konkurentnosta za pristap za pecatenje, mozno e nekoja od porakite da ne e na soodvetnoto mesto.");
  246.             System.out.println("Log na izvrsuvanje na akciite:");
  247.             System.out.println("=========================");
  248.             System.out.println("(tip m<=>Masko, tip z<=>Zensko)");
  249.             System.out.println("tip\tid\takcija/error");
  250.             System.out.println("=========================");
  251.             for (String l : actions) {
  252.                 System.out.println(l);
  253.             }
  254.         }
  255.  
  256.         public void printStatus() {
  257.             if (!hasError) {
  258.                 int poeni = 25;
  259.                 System.out.println("Procesot e uspesno sinhroniziran");
  260.                 if (show.maxMaskiGarderoba == 1 || show.maxZenskiGarderoba == 1) {
  261.                     System.out.println("\t-no ima maksimum eden ucesnik vo garderobata.");
  262.                     poeni -= 5;
  263.                 }
  264.                 if (show.maxTancuvanja == 1) {
  265.                     System.out.println("\t-no ima maksimum edna proverka vo eden moment.");
  266.                     poeni -= 5;
  267.                 }
  268.  
  269.                 System.out.println("Osvoeni poeni: " + poeni);
  270.  
  271.             } else {
  272.                 System.out.println("Procesot ne e sinhroniziran spored uslovite na zadacata");
  273.                 show.printLog();
  274.                 System.out.println("Maksimum mozni poeni: 15");
  275.             }
  276.  
  277.         }
  278.         private List<String> actions = new ArrayList<>();
  279.         private boolean hasError = false;
  280.     }
  281.     // Konstanti
  282.     public static int BROJ_INSTANCI = 1000;
  283.     public static final Random RANDOM = new Random();
  284.     public static final int RANDOM_RANGE = 3;
  285.     // Instanca od bafferot
  286.     public Show show;
  287.     public boolean hasException = false;
  288. }
Add Comment
Please, Sign In to add comment