AnimeFan666
AnimeFan666

Reputation: 15

Java: How can I have one thread add to a queue and another operate on it using threads?

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?

Upvotes: 1

Views: 2541

Answers (2)

LINEMAN78
LINEMAN78

Reputation: 2562

A lot of this work has been done for you if you use a thread-safe Queue implementation. I suggest the ArrayBlockingQueue. The only minor detail, which has advantages and disadvantages is that implementation has a fixed size unlike and ArrayList which can grow infinitely.

public abstract class AbstractQueuedRunnable<T> extends Thread
{
    protected BlockingQueue<T> queue;

    public AbstractQueuedRunnable(int maxQueueSize)
    {
        queue = new ArrayBlockingQueue<T>(maxQueueSize);
    }

    @Override
    public void run()
    {
        while (true)
        {
            try
            {
                T data = queue.take();
                doNext(data);
            }
            catch (Exception e)
            {
                if (e instanceof InterruptedException)
                    Thread.currentThread().interrupt();
                else
                    e.printStackTrace();
            }
        }
    }

    public void add(T data) throws InterruptedException
    {
        queue.put(data);
    }

    protected abstract void doNext(T data) throws Exception;
}

Upvotes: 0

Raunak
Raunak

Reputation: 6505

There's a class in java called ArrayBlockingQueue which does what you are trying to do and it is thread safe.

public BlockingQueue<Point> pointQueue = new ArrayBlockingQueue<Point>();

Upvotes: 1

Related Questions