Advertisement
Martina312

Untitled

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