Java Reference
In-Depth Information
for (String key : syncMap.keySet())
System.out.println(key + ": " + syncMap.get(key));
}
If you use an unsynchronized collection concurrently the result is undefi-
nedif you are lucky the error will be detected by the collection and you
will get a ConcurrentModificationException . If you are unlucky the collec-
tion will quietly become corrupt.
21.11.2. The Concurrent Collections
The java.util.concurrent subpackage provides collection implementa-
tions that are not only safe for concurrent use but are specially designed
to support such use.
When using a collection concurrently, you can't be sure when a collec-
tion might be empty, or, for a capacity constrained collection, when it
might be full. In such circumstances it is useful to be able to wait until
an element appears or until space for an element appears. The Blockin-
gQueue<E> interface that extends Queue<E> defines such a capability:
public void put(E elem) throws InterruptedException
Adds the specified element to this queue, waiting if necessary
for space to become available.
public boolean offer(E elem, long time, TimeUnit unit) tHRows Inter-
ruptedException
Adds the specified element to this queue, waiting if necessary
up to the specified waiting time for space to become available.
Returns TRue if the element was added, and false if the speci-
fied waiting time elapsed before space became available.
public E take() throws InterruptedException
 
Search WWH ::




Custom Search