Instead of instantiating the queue with Queue queue = new Queue(); write: Queue queue = Queue.Synchronized(new Queue());
Most of the framework's collections classes have this Synchronized method. Be sure to read the help for Synchronized, because iterating over a synchronized queue will fail if another thread changes the queue. |