Facebook
From Chartreuse Plover, 5 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 179
  1. import java.util.concurrent.Semaphore;
  2.  
  3. public class Main {
  4.     static Semaphore elementy = new Semaphore(0);
  5.     static Semaphore miejsca = new Semaphore(400);
  6.  
  7.     public static void main(String[] args) throws InterruptedException {
  8.         int m = 4;
  9.         int n = 5;
  10.         int powt1 = 100;
  11.         int powt2 = 80;
  12.         Bufor buff = new Bufor(m*powt1, elementy, miejsca);
  13.  
  14.         Producent[] P = new Producent[m];
  15.         Konsument[] K = new Konsument[n];
  16.  
  17.         for (int i = 0; i < 4; i++) {
  18.             P[i] = new Producent("P - " + i, buff, powt1);
  19.             P[i].start();
  20.         }
  21.  
  22.         for (int i = 0; i < 5; i++) {
  23.             K[i] = new Konsument("K - " + i, buff, powt2);
  24.             K[i].start();
  25.         }
  26.  
  27.         for (int i = 0; i < m; i++) {
  28.             P[i].join();
  29.         }
  30.  
  31.         for (int i = 0; i < n; i++) {
  32.             K[i].join();
  33.         }
  34.  
  35.         System.out.println("KONIEC PROGRAMU");
  36.  
  37.     }
  38. }