Advertisement
Latkoski

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

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