Crazy

Танц со студентите

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