The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.lang  [58 examples] > Threads  [11 examples]

e102. Implementing a Work Queue

The work queue is thread-safe so that multiple threads can simultaneously add and remove objects from it.
    class WorkQueue {
        LinkedList queue = new LinkedList();
        public synchronized void addWork(Object o) {
            queue.addLast(o);
            notify();
        }
        public synchronized Object getWork() throws InterruptedException {
            while (queue.isEmpty()) {
                wait();
            }
            return queue.removeFirst();
        }
    }

 Related Examples
e92. Creating a Thread
e93. Stopping a Thread
e94. Determining When a Thread Has Finished
e95. Pausing the Current Thread
e96. Pausing a Thread
e97. Determining If the Current Thread Is Holding a Synchronized Lock
e98. Allowing an Application with Live Threads to Exit
e99. Listing All Running Threads
e100. Using a Thread-Local Variable
e101. Getting the Stack Trace of an Exception

See also: Arrays    Assertions    Classes    Commands    Numbers    Objects    Strings    System Properties   


© 2002 Addison-Wesley.