Concurrent versus synchronized collections

The obvious question is What is the difference between a concurrent and a synchronized collection? Well, the main difference consists of the way in which they achieve thread-safety. Concurrent collections achieve thread-safety by partitioning the data into segments. Threads can access these segments concurrently and obtain locks only on the segments that are used. On the other hand, synchronized collection locks the entire collection via intrinsic locking (a thread that invokes a synchronized method will automatically acquire the intrinsic lock for that method's object and release it when the method returns).

Iterating a synchronized collection requires manual synchronization as follows:

List syncList = Collections.synchronizedList(new ArrayList());
...
synchronized(syncList) {
Iterator i = syncList.iterator();
while (i.hasNext()) {
do_something_with i.next();
}
}
Since concurrent collections allow concurrent access of threads, they are much more performant than the synchronized collection.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.34.25