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 presoblekuvalna;
- public static Semaphore spremni;
- public static Semaphore gotovi;
- static int brojIgraci;
- static int brSobleceni;
- public static void init() {
- sala=new Semaphore(12);
- presoblekuvalna=new Semaphore(4);
- spremni=new Semaphore(0);
- gotovi=new Semaphore(0);
- brojIgraci=0;
- brSobleceni=0;
- }
- public static class Player extends TemplateThread {
- public Player(int numRuns) {
- super(numRuns);
- }
- @Override
- public void execute() throws InterruptedException {
- sala.acquire();
- state.vlezi();
- synchronized(state)
- {
- brojIgraci++;
- if(brojIgraci==12)
- {
- spremni.release(12);
- }
- }
- spremni.acquire();//tuka gi blokiram ako ne se 12, odnosno tuka zastanuvaat site
- state.sportuvaj();//tuka koga kje dojdat 12 gi pushtam naednash site da kazat state.sportuvaj!!!
- presoblekuvalna.acquire(); // Toa e tuka sto ti kazhav za deka ne mora da se 4ca no mora da se povejke od 2
- state.presobleci();
- presoblekuvalna.release();
- synchronized(state)
- {
- brojIgraci--;
- if(brojIgraci==0)
- {
- state.slobodnaSala();
- sala.release(12);
- }
- }
- }
- }
- 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