public class MyBlockingQueue_Sync {
private Queue items = new LinkedList<>();
private Object lock = new Object();
public void put(String item) {
synchronized(lock) {
System.out.println("Writing");
this.items.add(item);
this.lock.notify();
System.out.println("Written");
}
}
public String get() {
synchronized(lock) {
System.out.println("Reading");
if(this.items.size() == 0) {
this.waitforput();
}
String item = this.items.poll();
System.out.println("Read");
return item;
}
}
void waitforput() {
try {
this.lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}