Advertisement
Latkoski

SchoolBus sync

Jun 19th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package kol2015g1;
  2.  
  3. import java.util.Date;
  4. import java.util.HashSet;
  5. import java.util.concurrent.Semaphore;
  6. import problem.ProblemExecution;
  7. import problem.TemplateThread;
  8.  
  9. /**
  10.  *
  11.  * @author ristes
  12.  *
  13.  */
  14.  
  15. public class SchoolBusSolution {
  16.     public static Semaphore blokirajStudentiVlez;
  17.     public static Semaphore blokirajStudentiVlez1;
  18.     public static Semaphore blokirajStudentIzlez;
  19.     public static Semaphore blokirajVozac;
  20.     public static Semaphore blokirajTrgvenje;
  21.     public static Semaphore lock;
  22.     public static Semaphore edinstvenVozac;
  23.     public static int brojac;
  24.  
  25.     public static void init() {
  26.         blokirajStudentiVlez = new Semaphore(0);
  27.         blokirajStudentiVlez1 = new Semaphore(0);
  28.         blokirajStudentIzlez = new Semaphore(0);
  29.         blokirajVozac = new Semaphore(0);
  30.         lock = new Semaphore(1);
  31.         brojac = 0;
  32.         edinstvenVozac = new Semaphore(1);
  33.         blokirajTrgvenje = new Semaphore(0);
  34.     }
  35.  
  36.     public static class Driver extends TemplateThread {
  37.  
  38.         public Driver(int numRuns) {
  39.             super(numRuns);
  40.         }
  41.  
  42.         @Override
  43.         public void execute() throws InterruptedException {
  44.             edinstvenVozac.acquire();
  45.            
  46.             lock.acquire();
  47.             state.driverEnter();
  48.             blokirajStudentiVlez.release(50);
  49.             lock.release();
  50.            
  51.             blokirajTrgvenje.acquire();
  52.             state.busDeparture();
  53.             state.busArrive();
  54.             blokirajStudentIzlez.release(50);
  55.            
  56.             blokirajVozac.acquire();
  57.             state.driverLeave();
  58.             edinstvenVozac.release();
  59.         }
  60.     }
  61.  
  62.     public static class Student extends TemplateThread {
  63.  
  64.         public Student(int numRuns) {
  65.             super(numRuns);
  66.         }
  67.  
  68.         @Override
  69.         public void execute() throws InterruptedException {
  70.             blokirajStudentiVlez.acquire();
  71.            
  72.            
  73.             lock.acquire();
  74.             brojac++;
  75.             state.studentEnter();
  76.             if (brojac == 50) {
  77.                 blokirajTrgvenje.release();
  78.             }
  79.             lock.release();
  80.            
  81.            
  82.             blokirajStudentIzlez.acquire();
  83.        
  84.            
  85.             lock.acquire();
  86.             state.studentLeave();
  87.             brojac--;
  88.             if (brojac == 0){
  89.                 blokirajVozac.release();
  90.             }
  91.             lock.release();
  92.            
  93.            
  94.         }
  95.     }
  96.  
  97.     static SchoolBusState state = new SchoolBusState();
  98.  
  99.     public static void main(String[] args) {
  100.         for (int i = 0; i < 10; i++) {
  101.             run();
  102.         }
  103.     }
  104.  
  105.     public static void run() {
  106.         try {
  107.             int numRuns = 1;
  108.             int numScenarios = 50;
  109.             HashSet<Thread> threads = new HashSet<Thread>();
  110.  
  111.             for (int i = 0; i < numScenarios; i++) {
  112.                 Student p = new Student(numRuns);
  113.                 threads.add(p);
  114.                 if (i % 50 == 0) {
  115.                     Driver c = new Driver(numRuns);
  116.                     threads.add(c);
  117.                 }
  118.             }
  119.  
  120.             init();
  121.  
  122.             ProblemExecution.start(threads, state);
  123.             System.out.println(new Date().getTime());
  124.         } catch (Exception ex) {
  125.             ex.printStackTrace();
  126.         }
  127.  
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement