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 mk.ukim.finki.os.synchronization.ProblemExecution;
- import mk.ukim.finki.os.synchronization.TemplateThread;
- public class Gym2Synchronization {
- public static Semaphore sala;
- public static Semaphore soblekuvalna;
- public static Semaphore lock;
- public static Semaphore blokirajIgraci;
- public static Semaphore blokirajSoblekuvalna;
- public static int brojac_sala;
- public static int brojac_soblekuvalna;
- public static Semaphore blokirajIzlez;
- public static int vkupen_brojac;
- public static Semaphore mutex;
- public static void init() {
- sala = new Semaphore(12);
- soblekuvalna = new Semaphore(4);
- lock = new Semaphore(1);
- blokirajIgraci = new Semaphore(0);
- blokirajIzlez = new Semaphore(0);
- blokirajSoblekuvalna = new Semaphore(0);
- brojac_sala = 0;
- brojac_soblekuvalna = 0;
- vkupen_brojac = 0;
- mutex = new Semaphore(0);
- }
- public static class Player extends TemplateThread {
- public Player(int numRuns) {
- super(numRuns);
- }
- @Override
- public void execute() throws InterruptedException {
- sala.acquire();
- lock.acquire();
- state.vlezi();
- brojac_sala++;
- if (brojac_sala == 12) {
- blokirajIgraci.release(12);
- }
- lock.release();// vlegvenje vo sala i presoblekuvanje
- blokirajIgraci.acquire(); // gi blokirat dur da se 12 i tie 12 da
- // vlezet da igret
- state.sportuvaj(); // sportuvat i ko so zavrsvit ojt da se
- // presoblekvit
- soblekuvalna.acquire();// odma se presoblekvit i izlegvit
- state.presobleci();
- soblekuvalna.release();
- lock.acquire();// gi namalvit site so zavrsile so igranje
- brojac_sala--;
- if (brojac_sala == 0) {
- state.slobodnaSala();// ko ke stignit na 0 oslobodvit sve
- sala.release(12);
- }
- lock.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 = 12;
- 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