Advertisement
Guest User

Untitled

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