Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package mk.ukim.finki.os.synchronization.exam16.k1.g2;
  2.  
  3. import mk.ukim.finki.os.synchronization.ProblemExecution;
  4. import mk.ukim.finki.os.synchronization.TemplateThread;
  5.  
  6. import java.util.HashSet;
  7. import java.util.concurrent.Semaphore;
  8.  
  9. public class CarnivalSolution {
  10.  
  11.     private static Semaphore bina=null;
  12.     private static Semaphore lock=null;
  13.     private static Semaphore dozvolaPrezentiranje=null;
  14.     public static int vkupno=0;
  15.     public static int brBina=0;
  16.     private static Semaphore novo=null;
  17.    
  18.  
  19.     public static void init() {
  20.         bina=new Semaphore(10);
  21.         lock=new Semaphore(1);
  22.         dozvolaPrezentiranje=new Semaphore(0);
  23.         novo=new Semaphore(0);
  24.        
  25.     }
  26.  
  27.     public static class Participant extends TemplateThread {
  28.  
  29.         public Participant(int numRuns) {
  30.             super(numRuns);
  31.         }
  32.  
  33.         @Override
  34.         public void execute() throws InterruptedException {
  35.            
  36.             bina.acquire();
  37.             state.participantEnter();
  38.             lock.acquire();
  39.             brBina++;
  40.             if(brBina==10)
  41.             {
  42.                 //brBina=0;
  43.                 dozvolaPrezentiranje.release(10);
  44.             }
  45.             lock.release();
  46.             dozvolaPrezentiranje.acquire();
  47.             state.present();
  48.            
  49.             lock.acquire();
  50.             vkupno++;
  51.             brBina--;
  52.            
  53.             if(brBina==0)
  54.             {
  55.                 state.endGroup();
  56.                 bina.release(10);
  57.             }
  58.            
  59.             if(vkupno==30)
  60.             {
  61.                 state.endCycle();
  62.                 vkupno=0;
  63.                 novo.release(30);
  64.             }
  65.             lock.release();
  66.             novo.acquire();
  67.            
  68.         }
  69.  
  70.     }
  71.  
  72.     public static void main(String[] args) {
  73.         for (int i = 0; i < 10; i++) {
  74.             run();
  75.         }
  76.     }
  77.  
  78.     static CarnivalState state = new CarnivalState();
  79.  
  80.     public static void run() {
  81.         try {
  82.             int numRuns = 15;
  83.             int numThreads = 30;
  84.  
  85.             HashSet<Thread> threads = new HashSet<Thread>();
  86.  
  87.             for (int i = 0; i < numThreads; i++) {
  88.                 Participant c = new Participant(numRuns);
  89.                 threads.add(c);
  90.             }
  91.  
  92.             init();
  93.  
  94.             ProblemExecution.start(threads, state);
  95.         } catch (Exception ex) {
  96.             ex.printStackTrace();
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement