Advertisement
Guest User

Untitled

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