Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Tema2;
- import es.urjc.etsii.code.concurrency.SimpleSemaphore;
- import java.util.Vector;
- import static es.urjc.etsii.code.concurrency.SimpleConcurrent.*;
- public class BufferInfinito {
- private static SimpleSemaphore bufferSemaphore = new SimpleSemaphore(0);
- private static volatile Vector<Integer> buffer = new Vector<>();
- public static void producer() {
- int i = 0;
- while(true) {
- println("Produced " + i);
- buffer.add(i++);
- bufferSemaphore.release();
- }
- }
- public static void consumer() {
- int pos = 0;
- while(true) {
- sleepRandom(500);
- bufferSemaphore.acquire();
- println("Consumed " + buffer.get(pos++));
- }
- }
- public static void main(String[] args) {
- createThread("producer");
- createThread("consumer");
- startThreadsAndWait();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement