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 mk.ukim.finki.os.synchronization.ProblemExecution;
- import mk.ukim.finki.os.synchronization.TemplateThread;
- public class Gym2Synchronization {
- public static Semaphore vlez_site;
- public static Semaphore sportuvaj;
- public static Semaphore sobleci_se;
- public static Semaphore kabina_eden;
- public static Semaphore mutex;
- public static int brojac = 0;
- public static void init() {
- vlez_site = new Semaphore(12);
- sportuvaj = new Semaphore(0);
- sobleci_se = new Semaphore(4);
- mutex = new Semaphore(1);
- }
- public static class Player extends TemplateThread {
- public Player(int numRuns) {
- super(numRuns);
- }
- @Override
- public void execute() throws InterruptedException {
- vlez_site.acquire();
- state.vlezi();
- mutex.acquire();
- brojac++;
- if(brojac==12)
- sportuvaj.release(12);
- mutex.release();
- sportuvaj.acquire();
- state.sportuvaj();
- sobleci_se.acquire();
- state.presobleci();
- sobleci_se.release();
- mutex.acquire();
- brojac--;
- if(brojac==0){
- state.slobodnaSala();
- vlez_site.release(12);
- }
- mutex.release();
- }
- }
- 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