Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 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. import java.util.concurrent.locks.Lock;
  8. import java.util.concurrent.locks.ReentrantLock;
  9.  
  10. import mk.ukim.finki.os.synchronization.ProblemExecution;
  11. import mk.ukim.finki.os.synchronization.TemplateThread;
  12.  
  13. public class Gym2Synchronization {
  14.  
  15.     static Semaphore igrac;
  16.     static int countIgrac;
  17.     static Lock lock;
  18.     static int br;
  19.    
  20.     static Semaphore igra,kabina,zatvori;
  21.    
  22.     public static void init() {
  23.         igrac = new Semaphore(12);
  24.         countIgrac = 0;
  25.         lock = new ReentrantLock();
  26.         br=0;
  27.         igra=new Semaphore(0);
  28.         kabina=new Semaphore(4);       
  29.     }
  30.    
  31.  
  32.     public static class Player extends TemplateThread {
  33.  
  34.         public Player(int numRuns) {
  35.             super(numRuns);
  36.         }
  37.  
  38.         @Override
  39.         public void execute() throws InterruptedException {
  40.             igrac.acquire();
  41.             state.vlezi();
  42.             lock.lock();
  43.            
  44.            
  45.             countIgrac++;
  46.             System.out.println("vleze "+ countIgrac);
  47.            
  48.         //  System.out.println(countIgrac);
  49.             if(countIgrac==12){
  50.                
  51.                 igra.release(12);
  52.                 System.out.println("oslobdi 12");
  53.                
  54.                 //System.out.println("IGRAME "+ countIgrac);
  55.             }
  56.             lock.unlock();
  57.            
  58.             igra.acquire();
  59.             System.out.println(igra);
  60.             state.sportuvaj();
  61.             System.out.println("sportuvat");
  62.            
  63.            
  64.             kabina.acquire();
  65.             state.presobleci();
  66.            
  67.             lock.lock();
  68.             countIgrac--;
  69.            
  70.             System.out.println("presoblekva "+countIgrac);
  71.             br++;
  72.            
  73.             if(br==4)
  74.             {
  75.                 br=0;
  76.                 kabina.release(4);
  77.             }
  78.             if(countIgrac==0)
  79.             {
  80.                 System.out.println("oslobodi sala");
  81.                 state.slobodnaSala();
  82.                 igrac.release(12);
  83.             }
  84.             lock.unlock();
  85.            
  86.         }
  87.     }
  88.    
  89.    
  90.     static Gym2State state = new Gym2State();
  91.  
  92.     public static void main(String[] args) {
  93.         for (int i = 0; i < 10; i++) {
  94.             run();
  95.         }
  96.     }
  97.  
  98.     public static void run() {
  99.         try {
  100.             Scanner s = new Scanner(System.in);
  101.             int numRuns = 1;
  102.             int numIterations = 1200;
  103.             s.close();
  104.  
  105.             HashSet<Thread> threads = new HashSet<Thread>();
  106.  
  107.             for (int i = 0; i < numIterations; i++) {
  108.                 Player h = new Player(numRuns);
  109.                 threads.add(h);
  110.             }
  111.  
  112.             init();
  113.  
  114.             ProblemExecution.start(threads, state);
  115.             System.out.println(new Date().getTime());
  116.         } catch (Exception ex) {
  117.             ex.printStackTrace();
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement