Advertisement
add1ctus

Dancing with the students

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