Advertisement
ilevishinov

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

Mar 25th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.41 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.Scanner;
  6. import java.util.concurrent.Semaphore;
  7.  
  8. /**
  9.  *
  10.  * @author ristes + Bani57
  11.  */
  12. public class TancSoStudentite {
  13.     //TODO: Definicija na globalni promenlivi i semafori
  14.     public static Semaphore dancingHall;
  15.     public static Semaphore menDressingRoom;
  16.     public static Semaphore womenDressingRoom;
  17.     public static Semaphore childrenDressingRoom;
  18.     public static int freeMen;
  19.     public static Semaphore countMen;
  20.     public static Semaphore singleLady;
  21.     public static Semaphore letsDance;
  22.  
  23.     public void init() {
  24.         //TODO: da se implementira
  25.         menDressingRoom = new Semaphore(10);
  26.         womenDressingRoom = new Semaphore(10);
  27.         childrenDressingRoom = new Semaphore(10);
  28.         dancingHall = new Semaphore(3);
  29.         freeMen = 0;
  30.         countMen = new Semaphore(1);
  31.         singleLady = new Semaphore(0);
  32.        
  33.     }
  34.  
  35.     class Masko extends Thread {
  36.         //TODO: Definicija  na promenlivi za sostojbata
  37.  
  38.         public void ucestvo() throws InterruptedException {
  39.             //TODO: Kod za mashkite
  40.             menDressingRoom.acquire();
  41.             show.presobleci();
  42.             menDressingRoom.release();
  43.            
  44.             countMen.acquire();
  45.                 freeMen++;            
  46.             countMen.release();
  47.            
  48.             singleLady.acquire();
  49.  
  50.             dancingHall.acquire();
  51.             show.tancuvaj();
  52.             dancingHall.release();
  53.         }
  54.  
  55.         @Override
  56.         public void run() {
  57.             try {
  58.                 ucestvo();
  59.             } catch (InterruptedException e) {
  60.                 // Do nothing
  61.             } catch (Exception e) {
  62.                 exception = e;
  63.                 hasException = true;
  64.             }
  65.         }
  66.  
  67.         @Override
  68.         public String toString() {
  69.             return String.format("m\t%d", getId());
  70.         }
  71.         public Exception exception = null;
  72.     }
  73.  
  74.     class Zensko extends Thread {
  75.         //TODO: Definicija  na promenlivi za sostojbata
  76.  
  77.         public void ucestvo() throws InterruptedException {
  78.            //TODO: Kod za zenskite
  79.             womenDressingRoom.acquire();
  80.             show.presobleci();
  81.             womenDressingRoom.release();
  82.            
  83.             singleLady.release();
  84.            
  85.  
  86.         }
  87.  
  88.         @Override
  89.         public void run() {
  90.             try {
  91.                 ucestvo();
  92.             } catch (InterruptedException e) {
  93.                 // Do nothing
  94.             } catch (Exception e) {
  95.                 exception = e;
  96.                 hasException = true;
  97.             }
  98.         }
  99.  
  100.         @Override
  101.         public String toString() {
  102.             return String.format("z\t%d", getId());
  103.         }
  104.         public Exception exception = null;
  105.     }
  106.  
  107.     class Dete extends Thread {
  108.         //TODO: Definicija  na promenlivi za sostojbata
  109.  
  110.         public void ucestvo() throws InterruptedException {
  111.             //TODO: Kod za decata
  112.             childrenDressingRoom.acquire();
  113.             show.presobleci();
  114.             childrenDressingRoom.release();
  115.            
  116.             countMen.acquire();
  117.                 if(freeMen > 0) {
  118.                     singleLady.release();
  119.                 } else {
  120.                     singleLady.acquire();
  121.                     dancingHall.acquire();
  122.                      show.tancuvaj();
  123.                     dancingHall.release();
  124.                 }
  125.             countMen.release();
  126.            
  127.            
  128.            
  129.             // Go povikuvaat samo deca sto ste pretstavuvaat kako masko
  130.            
  131.         }
  132.  
  133.         @Override
  134.         public void run() {
  135.             try {
  136.                 ucestvo();
  137.             } catch (InterruptedException e) {
  138.                 // Do nothing
  139.             } catch (Exception e) {
  140.                 exception = e;
  141.                 hasException = true;
  142.             }
  143.         }
  144.  
  145.         @Override
  146.         public String toString() {
  147.             return String.format("t\t%d", getId());
  148.         }
  149.         public Exception exception = null;
  150.     }
  151.  
  152.     public static void main(String[] args) {
  153.         try {
  154.             TancSoStudentite environment = new TancSoStudentite();
  155.             environment.start();
  156.         } catch (Exception ex) {
  157.             ex.printStackTrace();
  158.         }
  159.     }
  160.  
  161.     public void start() throws Exception {
  162.         show = new Show();
  163.         init();
  164.         HashSet<Thread> threads = new HashSet<>();
  165.         Scanner scanner = new Scanner(System.in);
  166.         int BROJ_INSTANCI = scanner.nextInt();
  167.         scanner.close();
  168.         for (int i = 0; i < BROJ_INSTANCI; i++) {
  169.             Zensko z = new Zensko();
  170.             Masko m = new Masko();
  171.             threads.add(z);
  172.             threads.add(m);
  173.             Dete t=new Dete();
  174.             threads.add(t);
  175.             t=new Dete();
  176.             threads.add(t);
  177.         }
  178.  
  179.         for (Thread t : threads) {
  180.             t.start();
  181.         }
  182.  
  183.         for (Thread t : threads) {
  184.             if (!hasException) {
  185.                 t.join();
  186.             } else {
  187.                 t.interrupt();
  188.             }
  189.         }
  190.         show.printStatus();
  191.  
  192.     }
  193.  
  194.  
  195.     public class Show {
  196.  
  197.         public static final int BROJ_GARDEROBA = 10;
  198.         public static final int BROJ_TEREN = 3;
  199.         public static final int TYPE_MASKO = 1;
  200.         public static final int TYPE_ZENSKO = 2;
  201.         public static final int TYPE_UNKNOWN = -1;
  202.  
  203.         public Show() {
  204.         }
  205.         public int brojMaskiGarderoba = 0;
  206.         public int brojZenskiGarderoba = 0;
  207.         public int brojDetskaGarderoba = 0;
  208.         public int brojTancuvanja = 0;
  209.         public int maxMaskiGarderoba = 0;
  210.         public int maxZenskiGarderoba = 0;
  211.         public int maxDetskaGarderoba = 0;
  212.         public int maxTancuvanja = 0;
  213.  
  214.         public void presobleci() throws RuntimeException {
  215.             log(null, "presobleci start");
  216.             Thread t = Thread.currentThread();
  217.             if (t instanceof Masko) {
  218.                 synchronized (RANDOM) {
  219.                     brojMaskiGarderoba++;
  220.                     if (brojMaskiGarderoba > 10) {
  221.                         exception("Ne moze da ima poveke od 10 maski vo maskata garderoba.");
  222.                     }
  223.                     if (brojMaskiGarderoba > maxMaskiGarderoba) {
  224.                         maxMaskiGarderoba = brojMaskiGarderoba;
  225.                     }
  226.                 }
  227.                 waitRandom();
  228.                 synchronized (RANDOM) {
  229.                     brojMaskiGarderoba--;
  230.                 }
  231.             } else if (t instanceof Zensko){
  232.                 synchronized (RANDOM) {
  233.                     brojZenskiGarderoba++;
  234.                     if (brojZenskiGarderoba > 10) {
  235.                         exception("Ne moze da ima poveke od 10 zenski vo zenskata garderoba.");
  236.                     }
  237.                     if (brojZenskiGarderoba > maxZenskiGarderoba) {
  238.                         maxZenskiGarderoba = brojZenskiGarderoba;
  239.                     }
  240.                 }
  241.                 waitRandom();
  242.                 synchronized (RANDOM) {
  243.                     brojZenskiGarderoba--;
  244.                 }
  245.             }
  246.             else {
  247.                 synchronized (RANDOM) {
  248.                     brojDetskaGarderoba++;
  249.                     if (brojDetskaGarderoba > 10) {
  250.                         exception("Ne moze da ima poveke od 10 deca vo detskata garderoba.");
  251.                     }
  252.                     if (brojDetskaGarderoba > maxDetskaGarderoba) {
  253.                         maxDetskaGarderoba = brojDetskaGarderoba;
  254.                     }
  255.                 }
  256.                 waitRandom();
  257.                 synchronized (RANDOM) {
  258.                     brojDetskaGarderoba--;
  259.                 }
  260.             }
  261.             log(null, "presobleci kraj");
  262.         }
  263.  
  264.         public void tancuvaj() throws RuntimeException {
  265.             log(null, "tancuvaj start");
  266.             synchronized (RANDOM) {
  267.                 brojTancuvanja++;
  268.                 if (brojTancuvanja > BROJ_TEREN) {
  269.                     exception("Ne moze paralelno da tancuvaat poveke od 3 para.");
  270.                 }
  271.  
  272.                 if (brojTancuvanja > maxTancuvanja) {
  273.                     maxTancuvanja = brojTancuvanja;
  274.                 }
  275.             }
  276.             waitRandom();
  277.             synchronized (RANDOM) {
  278.                 brojTancuvanja--;
  279.             }
  280.             log(null, "tancuvaj kraj");
  281.         }
  282.  
  283.         private void waitRandom() {
  284.             try {
  285.                 int r;
  286.                 synchronized (RANDOM) {
  287.                     r = RANDOM.nextInt(RANDOM_RANGE);
  288.                 }
  289.                 Thread.sleep(r);
  290.             } catch (Exception e) {
  291.                 //do nothing
  292.             }
  293.         }
  294.  
  295.         private void exception(String message) {
  296.             RuntimeException e = new RuntimeException(message);
  297.             log(e, null);
  298.             hasError = true;
  299.             throw e;
  300.         }
  301.  
  302.         public int getType() {
  303.             Thread t = Thread.currentThread();
  304.             if (t instanceof Masko) {
  305.                 return TYPE_MASKO;
  306.             } else if (t instanceof Zensko) {
  307.                 return TYPE_ZENSKO;
  308.             } else {
  309.                 return TYPE_UNKNOWN;
  310.             }
  311.         }
  312.  
  313.         private synchronized void log(RuntimeException e, String action) {
  314.             Thread t = Thread.currentThread();
  315.             if (e == null) {
  316.                 actions.add(t.toString() + "\t(a): " + action);
  317.             } else {
  318.                 actions.add(t.toString() + "\t(e): " + e.getMessage());
  319.             }
  320.         }
  321.  
  322.         public synchronized void printLog() {
  323.             System.out.println("Poradi konkurentnosta za pristap za pecatenje, mozno e nekoja od porakite da ne e na soodvetnoto mesto.");
  324.             System.out.println("Log na izvrsuvanje na akciite:");
  325.             System.out.println("=========================");
  326.             System.out.println("(tip m<=>Masko, tip z<=>Zensko)");
  327.             System.out.println("tip\tid\takcija/error");
  328.             System.out.println("=========================");
  329.             for (String l : actions) {
  330.                 System.out.println(l);
  331.             }
  332.         }
  333.  
  334.         public void printStatus() {
  335.             if (!hasError) {
  336.                 int poeni = 25;
  337.                 System.out.println("Procesot e uspesno sinhroniziran");
  338.                 if (show.maxMaskiGarderoba == 1 || show.maxZenskiGarderoba == 1 || show.maxDetskaGarderoba == 1) {
  339.                     System.out.println("\t-no ima maksimum eden ucesnik vo garderobata.");
  340.                     poeni -= 5;
  341.                 }
  342.                 if (show.maxTancuvanja == 1) {
  343.                     System.out.println("\t-no ima maksimum edna proverka vo eden moment.");
  344.                     poeni -= 5;
  345.                 }
  346.  
  347.                 System.out.println("Osvoeni poeni: " + poeni);
  348.  
  349.             } else {
  350.                 System.out.println("Procesot ne e sinhroniziran spored uslovite na zadacata");
  351.                 show.printLog();
  352.                 System.out.println("Maksimum mozni poeni: 15");
  353.             }
  354.  
  355.         }
  356.         private List<String> actions = new ArrayList<>();
  357.         private boolean hasError = false;
  358.     }
  359.  
  360.     private static final Random RANDOM = new Random();
  361.     private static final int RANDOM_RANGE = 3;
  362.     // Instanca od bafferot
  363.     private Show show;
  364.     private boolean hasException = false;
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement