Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. package Tema2;
  2.  
  3. import es.urjc.etsii.code.concurrency.SimpleSemaphore;
  4.  
  5. import java.util.Vector;
  6.  
  7. import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
  8.  
  9. public class BufferInfinito {
  10.     private static SimpleSemaphore bufferSemaphore = new SimpleSemaphore(0);
  11.     private static volatile Vector<Integer> buffer = new Vector<>();
  12.  
  13.     public static void producer() {
  14.         int i = 0;
  15.         while(true) {
  16.             println("Produced " + i);
  17.             buffer.add(i++);
  18.             bufferSemaphore.release();
  19.         }
  20.     }
  21.  
  22.     public static void consumer() {
  23.         int pos = 0;
  24.         while(true) {
  25.             sleepRandom(500);
  26.             bufferSemaphore.acquire();
  27.             println("Consumed " + buffer.get(pos++));
  28.         }
  29.     }
  30.  
  31.     public static void main(String[] args) {
  32.         createThread("producer");
  33.         createThread("consumer");
  34.         startThreadsAndWait();
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement