While studying how to use synchronized buffers, I needed to use the Buffer interface in order to complete the assignment. I'm pretty sure my entire code is correct with the exception of how I implemented the buffer. I got an error code on my public class saying "Interface expected here." The hints are telling me to extend Buffer instead of implement it. Does anyone have any ideas as to what I am missing? (This is 1 out of 4 classes).
package synchronizedbuffer;
public class SynchronizedBuffer implements Buffer {
private int buffer = -1;
private boolean occupied = false;
public synchronized void put(int value) throws InterruptedException {
while(occupied) {
System.out.println("Producer tries to write.");
displayState("Buffer full. Producer waits");
wait();
}
buffer = value;
occupied = true;
displayState("Producer writes " + buffer);
notifyAll();
}
public synchronized int get() throws InterruptedException {
while(!occupied) {
System.out.println("Consumer tries to read");
displayState("Buffer empty. Consumer waits");
wait();
}
occupied = false;
displayState("Consumer reads" + buffer);
notifyAll();
return buffer;
}
private synchronized void displayState(String operation) {
System.out.printf("%-40s%d\t\t%b%n%n", operation, buffer, occupied);
}
}
EDIT:: I editted the code to remove the import java.nio.Buffer;