Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mk.ukim.finki.os.synchronization.exam16.k1.g2;
- import mk.ukim.finki.os.synchronization.ProblemExecution;
- import mk.ukim.finki.os.synchronization.TemplateThread;
- import java.util.HashSet;
- import java.util.concurrent.Semaphore;
- public class CarnivalSolution {
- private static Semaphore bina=null;
- private static Semaphore lock=null;
- private static Semaphore dozvolaPrezentiranje=null;
- public static int vkupno=0;
- public static int brBina=0;
- private static Semaphore novo=null;
- public static void init() {
- bina=new Semaphore(10);
- lock=new Semaphore(1);
- dozvolaPrezentiranje=new Semaphore(0);
- novo=new Semaphore(0);
- }
- public static class Participant extends TemplateThread {
- public Participant(int numRuns) {
- super(numRuns);
- }
- @Override
- public void execute() throws InterruptedException {
- bina.acquire();
- state.participantEnter();
- lock.acquire();
- brBina++;
- if(brBina==10)
- {
- //brBina=0;
- dozvolaPrezentiranje.release(10);
- }
- lock.release();
- dozvolaPrezentiranje.acquire();
- state.present();
- lock.acquire();
- vkupno++;
- brBina--;
- if(brBina==0)
- {
- state.endGroup();
- bina.release(10);
- }
- if(vkupno==30)
- {
- state.endCycle();
- vkupno=0;
- novo.release(30);
- }
- lock.release();
- novo.acquire();
- }
- }
- public static void main(String[] args) {
- for (int i = 0; i < 10; i++) {
- run();
- }
- }
- static CarnivalState state = new CarnivalState();
- public static void run() {
- try {
- int numRuns = 15;
- int numThreads = 30;
- HashSet<Thread> threads = new HashSet<Thread>();
- for (int i = 0; i < numThreads; i++) {
- Participant c = new Participant(numRuns);
- threads.add(c);
- }
- init();
- ProblemExecution.start(threads, state);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement