Advertisement
Latkoski

Gym

Jun 18th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package mk.ukim.finki.os.synchronization.exam14.june;
  2.  
  3. import java.util.Date;
  4. import java.util.HashSet;
  5. import java.util.Scanner;
  6. import java.util.concurrent.Semaphore;
  7.  
  8. import mk.ukim.finki.os.synchronization.ProblemExecution;
  9. import mk.ukim.finki.os.synchronization.TemplateThread;
  10.  
  11. public class Gym2Synchronization {
  12.  
  13.     public static Semaphore sala;
  14.     public static Semaphore soblekuvalna;
  15.     public static Semaphore lock;
  16.     public static Semaphore blokirajIgraci;
  17.     public static Semaphore blokirajSoblekuvalna;
  18.     public static int brojac_sala;
  19.     public static int brojac_soblekuvalna;
  20.     public static Semaphore blokirajIzlez;
  21.     public static int vkupen_brojac;
  22.     public static Semaphore mutex;
  23.  
  24.     public static void init() {
  25.         sala = new Semaphore(12);
  26.         soblekuvalna = new Semaphore(4);
  27.         lock = new Semaphore(1);
  28.         blokirajIgraci = new Semaphore(0);
  29.         blokirajIzlez = new Semaphore(0);
  30.         blokirajSoblekuvalna = new Semaphore(0);
  31.         brojac_sala = 0;
  32.         brojac_soblekuvalna = 0;
  33.         vkupen_brojac = 0;
  34.         mutex = new Semaphore(0);
  35.     }
  36.  
  37.     public static class Player extends TemplateThread {
  38.  
  39.         public Player(int numRuns) {
  40.             super(numRuns);
  41.         }
  42.  
  43.         @Override
  44.         public void execute() throws InterruptedException {
  45.             sala.acquire();
  46.  
  47.             lock.acquire();
  48.             state.vlezi();
  49.             brojac_sala++;
  50.             if (brojac_sala == 12) {
  51.                 blokirajIgraci.release(12);
  52.             }
  53.             lock.release();// vlegvenje vo sala i presoblekuvanje
  54.  
  55.             blokirajIgraci.acquire(); // gi blokirat dur da se 12 i tie 12 da
  56.                                         // vlezet da igret
  57.  
  58.             state.sportuvaj(); // sportuvat i ko so zavrsvit ojt da se
  59.                                 // presoblekvit
  60.             soblekuvalna.acquire();// odma se presoblekvit i izlegvit
  61.             state.presobleci();
  62.             soblekuvalna.release();
  63.  
  64.             lock.acquire();// gi namalvit site so zavrsile so igranje
  65.             brojac_sala--;
  66.             if (brojac_sala == 0) {
  67.                 state.slobodnaSala();// ko ke stignit na 0 oslobodvit sve
  68.                 sala.release(12);
  69.             }
  70.             lock.release();
  71.  
  72.         }
  73.     }
  74.  
  75.     static Gym2State state = new Gym2State();
  76.  
  77.     public static void main(String[] args) {
  78.         for (int i = 0; i < 10; i++) {
  79.             run();
  80.         }
  81.     }
  82.  
  83.     public static void run() {
  84.         try {
  85.             Scanner s = new Scanner(System.in);
  86.             int numRuns = 1;
  87.             int numIterations = 12;
  88.             s.close();
  89.  
  90.             HashSet<Thread> threads = new HashSet<Thread>();
  91.  
  92.             for (int i = 0; i < numIterations; i++) {
  93.                 Player h = new Player(numRuns);
  94.                 threads.add(h);
  95.             }
  96.  
  97.             init();
  98.  
  99.             ProblemExecution.start(threads, state);
  100.             System.out.println(new Date().getTime());
  101.         } catch (Exception ex) {
  102.             ex.printStackTrace();
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement