Here’s what I have:
public class Queue implements Runnable {
ArrayList<Point> queue;
public Queue(){
this.queue=new ArrayList<Point>();
}
synchronized public void cuePoint(Point p){
this.queue.add(p);
}
synchronized public void doFirstPoint(){
if(queue.size()!=0){
//some operation that takes a real long time
queue.remove(0);
}
}
synchronized public void clearQueue(){
this.queue.clear();
}
public void run() {
while(true){
doFirstPoint();
}
}
}
However, the problem with this code is that if the queue thread is working on a point in the queue (which, as noted, takes a real long time) the cuing thread is kept waiting. Is there a simple, intuitive way to fix this?
There’s a class in java called ArrayBlockingQueue which does what you are trying to do and it is thread safe.