Advertisement
Azure47

OS Lab 3.1

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