Thus, you can put objects into the queue as fast as you can type them in. Meanwhile, the various threads are printing the objects out. Each thread announces its thread ID when it prints out an object, so you can see that different threads are receiving the objects. The blocking queue is simple and useful. There are some potential problems that might happen, especially in complex or high-traffic systems.
Deadlock is a problem in any multithreaded program, and there's no simple solution for it.
Such a situation can simply be described as bad design. One modification that might take care of this is to modify the queue code to have a timeout, so that a thread will wait only a certain amount of time before the get method returns a null . Summary The blocking queue may not be right for all applications, but it is remarkably effective given the simplicity of its implementation. The blocking queue should be handy in the toolbox of any Java programmer who wants to write a multithreaded program. Greg Travis is a freelance programmer living in New York City.