Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mk.ukim.finki.os.synchronization.exam14.june;
- import java.util.Date;
- import java.util.HashSet;
- import java.util.Scanner;
- import java.util.concurrent.Semaphore;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- import mk.ukim.finki.os.synchronization.ProblemExecution;
- import mk.ukim.finki.os.synchronization.TemplateThread;
- public class Gym2Synchronization {
- static Semaphore igrac;
- static int countIgrac;
- static Lock lock;
- static int br;
- static Semaphore igra,kabina,zatvori;
- public static void init() {
- igrac = new Semaphore(12);
- countIgrac = 0;
- lock = new ReentrantLock();
- br=0;
- igra=new Semaphore(0);
- kabina=new Semaphore(4);
- }
- public static class Player extends TemplateThread {
- public Player(int numRuns) {
- super(numRuns);
- }
- @Override
- public void execute() throws InterruptedException {
- igrac.acquire();
- state.vlezi();
- lock.lock();
- countIgrac++;
- System.out.println("vleze "+ countIgrac);
- // System.out.println(countIgrac);
- if(countIgrac==12){
- igra.release(12);
- System.out.println("oslobdi 12");
- //System.out.println("IGRAME "+ countIgrac);
- }
- lock.unlock();
- igra.acquire();
- System.out.println(igra);
- state.sportuvaj();
- System.out.println("sportuvat");
- kabina.acquire();
- state.presobleci();
- lock.lock();
- countIgrac--;
- System.out.println("presoblekva "+countIgrac);
- br++;
- if(br==4)
- {
- br=0;
- kabina.release(4);
- }
- if(countIgrac==0)
- {
- System.out.println("oslobodi sala");
- state.slobodnaSala();
- igrac.release(12);
- }
- lock.unlock();
- }
- }
- static Gym2State state = new Gym2State();
- public static void main(String[] args) {
- for (int i = 0; i < 10; i++) {
- run();
- }
- }
- public static void run() {
- try {
- Scanner s = new Scanner(System.in);
- int numRuns = 1;
- int numIterations = 1200;
- s.close();
- HashSet<Thread> threads = new HashSet<Thread>();
- for (int i = 0; i < numIterations; i++) {
- Player h = new Player(numRuns);
- threads.add(h);
- }
- init();
- ProblemExecution.start(threads, state);
- System.out.println(new Date().getTime());
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement