Advertisement
Josif_tepe

Untitled

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