UFMG - Pós-graduação em Ciência da Computação - Programação Paralela

A seguir: Propriedades do Buffer Compartilhado Acima: Aula 11 - Modelos Anterior: Motivação


O Buffer Compartilhado

O Buffer Compartilhado

Código Java Simplificado
class Buffer {
  private Queue buffer;
  int n;
  final int MAX = 10;
  public Buffer() {
    buffer = new Queue(MAX); n = 0;
  }
  public synchronized void put(char x) {
    while(n >= MAX) {
        wait();
    }
    buffer.append(x); n++;
    notify();
  }
  public synchronized char get() {
    while (n <= 0){
        wait();
    }
    notify(); n--;
    return buffer.dequeue();
  }
}



 

Osvaldo Carvalho