Tag Archives: WorkQueue

WorkQueue

Introduction

Incoming requests from multiple clients are stored by connection handler threads in the work queue. The requests are then processed by the worker thread.  Note that this implementation uses a LinkedList which is synchronized to prevent multiple Threads to access and modify the LinkedList simultaneously.

public class  {
    LinkedList queue = new LinkedList();

    // Add work to the work queue
    public synchronized void addWork(Object o) {
        queue.addLast(o);
        notify();
    }

    // Retrieve work from the work queue; block if the queue is empty
    public synchronized Object getWork() throws InterruptedException {
        while (queue.isEmpty()) {
            wait();
        }
        return queue.removeFirst();
    }
}