Adobe Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
class ArrayBlockingQueue<E>{
static final int size=10;
ArrayList<E> arr= new ArrayList<E>(10);
synchronized void enqueue(E item) throws InterruptedException{
while(arr.size() == size){
wait();
}
if(arr.isEmpty()) {
notify();
}
arr.add(item);
}
synchronized E dequeue(int item) throws InterruptedException{
while(arr.isEmpty()){
wait();
}
if(arr.size()==size) {
notify();
}
return arr.remove(0);
}
}
use wait and notify
- Anonymous April 21, 2012