Advertisement
salercode

OS lab3zad1

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