Advertisement
Guest User

Tanc so studenti

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