daily pastebin goal
17%
SHARE
TWEET

Untitled

a guest Jan 13th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ispitSept2017;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.concurrent.Semaphore;
  7.  
  8.  
  9. public class Processor extends Thread{
  10.  
  11.   public static Random random = new Random();
  12.  
  13.   public static Semaphore event = new Semaphore(1);
  14.   public static Semaphore inGenerate = new Semaphore(5);
  15.  
  16.   static List<EventGenerator> scheduled = new ArrayList<>();
  17.  
  18.   public static void main(String[] args) throws InterruptedException {
  19.     // TODO: kreirajte Processor i startuvajte go negovoto pozadinsko izvrsuvanje
  20.       Processor processor=new Processor();
  21.       processor.start();
  22.  
  23.  
  24.     for (int i = 0; i < 100; i++) {
  25.       EventGenerator eventGenerator = new EventGenerator();
  26.       register(eventGenerator);
  27.       //TODO: startuvajte go eventGenerator-ot
  28.       eventGenerator.start();
  29.     }
  30.  
  31.     // TODO: Cekajte 20000ms za Processor-ot da zavrsi
  32.     processor.join(20000);
  33.  
  34.     // TODO: ispisete go statusot od izvrsuvanjeto
  35.     if (processor.isAlive()) {
  36.         processor.interrupt();
  37.         System.out.println("Terminated scheduling");
  38.     } else {
  39.         System.out.println("Finished scheduling");
  40.     }
  41.   }
  42.  
  43.   public static void register(EventGenerator generator) {
  44.     scheduled.add(generator);
  45.     try {
  46.       Thread.sleep(random.nextInt(100));
  47.     } catch (InterruptedException e) {
  48.       e.printStackTrace();
  49.     }
  50.     scheduled.remove(0);
  51.   }
  52.  
  53.   /**
  54.    * Ne smee da bide izvrsuva paralelno so generate() metodot
  55.    */
  56.   public static void process() {
  57.     System.out.println("processing event");
  58.   }
  59.  
  60.  
  61.   public void run() {
  62.  
  63.     while (!scheduled.isEmpty()) {
  64.         try {
  65.               // TODO: cekanje  na nov generiran event
  66.                 inGenerate.acquire(5);
  67.                 event.acquire();
  68.               // TODO: povikajte go negoviot process() metod
  69.                 process();
  70.                 inGenerate.release(5);
  71.         }
  72.         catch (Exception e) {
  73.               e.printStackTrace();
  74.         }
  75.     }
  76.  
  77.     System.out.println("Done scheduling!");
  78.   }
  79. }
  80.  
  81.  
  82. class EventGenerator extends Thread {
  83.    
  84.   public Integer duration;
  85.  
  86.   public EventGenerator() throws InterruptedException {
  87.     this.duration = Processor.random.nextInt(1000);
  88.   }
  89.    
  90.   /**
  91.    * Ne smee da bide povikan paralelno kaj poveke od 5 generatori
  92.    */
  93.   public static void generate() {
  94.         System.out.println("Generating event: ");
  95.   }
  96.  
  97.   public void run(){
  98.       try {
  99.           Thread.sleep(duration);
  100.           Processor.inGenerate.acquire();
  101.           generate();
  102.           Processor.event.release();
  103.           Processor.inGenerate.release();
  104.       }catch (InterruptedException e){
  105.           e.printStackTrace();
  106.       }
  107.   }
  108. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top