Advertisement
nocturnalmk

Untitled

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