Java Reference
In-Depth Information
For mutable objects, producer-consumer designs and blocking queues facilitate serial thread
confinement for handing off ownership of objects from producers to consumers. A thread-
confined object is owned exclusively by a single thread, but that ownership can be “trans-
ferred” by publishing it safely where only one other thread will gain access to it and ensuring
that the publishing thread does not access it after the handoff. The safe publication ensures
that the object's state is visible to the new owner, and since the original owner will not touch
it again, it is now confined to the new thread. The new owner may modify it freely since it
has exclusive access.
Object pools exploit serial thread confinement, “lending” an object to a requesting thread.
As long as the pool contains sufficient internal synchronization to publish the pooled object
safely, and as long as the clients do not themselves publish the pooled object or use it after
returning it to the pool, ownership can be transferred safely from thread to thread.
One could also use other publication mechanisms for transferring ownership of a mutable ob-
ject, but it is necessary to ensure that only one thread receives the object being handed off.
Blocking queues make this easy; with a little more work, it could also done with the atomic
remove method of ConcurrentMap or the compareAndSet method of AtomicRe-
ference .
Listing 5.8. Producer and Consumer Tasks in a Desktop Search Application.
Search WWH ::




Custom Search