aspose file tools*
The moose likes Beginning Java and the fly likes Collection.synchronizedList usage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collection.synchronizedList usage" Watch "Collection.synchronizedList usage" New topic
Author

Collection.synchronizedList usage

joni novhia
Greenhorn

Joined: Dec 11, 2011
Posts: 11




Output :
Reader thread started .................
Writer thread started .................
Element added :10
Element number 1 :1
Element number 2 :2
Element number 3 :3
Element added :11
Exception in thread "Reader" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at Operations.show(CollectionAlgo.java:20)
at Reader.run(CollectionAlgo.java:66)
at java.lang.Thread.run(Unknown Source)
Element added :12
Element added :13
Element added :14
Element added :15
Element added :16
Element added :17
Element added :18
Element added :19


-----------------------------------------

In above program, I used Collection.synchronizedList() method to get threadSafe collection. But still i am getting ConcurrentModificationException when run this program.
The above program just fires two thread , a reader and a writer to read and write to a List.

Can anyone help me to understand this weird behaviour of this method ?



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

shailesh pratapwar wrote:



Output :
Reader thread started .................
Writer thread started .................
Element added :10
Element number 1 :1
Element number 2 :2
Element number 3 :3
Element added :11
Exception in thread "Reader" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at Operations.show(CollectionAlgo.java:20)
at Reader.run(CollectionAlgo.java:66)
at java.lang.Thread.run(Unknown Source)
Element added :12
Element added :13
Element added :14
Element added :15
Element added :16
Element added :17
Element added :18
Element added :19


-----------------------------------------

In above program, I used Collection.synchronizedList() method to get threadSafe collection. But still i am getting ConcurrentModificationException when run this program.
The above program just fires two thread , a reader and a writer to read and write to a List.

Can anyone help me to understand this weird behaviour of this method ?



All the synchronizedList method does is provides a List whose methods are synchronized. This means that it is threadsafe for parallel *single* method calls. However, in the case of the iterator, it needs to be thread safe across multiple method calls.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Henry Wong wrote:
All the synchronizedList method does is provides a List whose methods are synchronized. This means that it is threadsafe for parallel *single* method calls. However, in the case of the iterator, it needs to be thread safe across multiple method calls.


One possible way around this... is to grab the collection lock first.



Of course, that means that nothing can happen until the iterator finishes first.

Henry
joni novhia
Greenhorn

Joined: Dec 11, 2011
Posts: 11
Henry Wong wrote:
Henry Wong wrote:
All the synchronizedList method does is provides a List whose methods are synchronized. This means that it is threadsafe for parallel *single* method calls. However, in the case of the iterator, it needs to be thread safe across multiple method calls.


One possible way around this... is to grab the collection lock first.



Of course, that means that nothing can happen until the iterator finishes first.

Henry





Hi Henry,
Thanks for the reply.

I agree with your comment. So, we can just infer that if we have an iterator then this Collections.synchronizedList() would be not useful. what say ?
Also, I wonder if there is any scenario where this Collections.synchronizedList() would be useful. Could you please demonstrate the use of this method with any program so that i could understand it perfectely.

Thanks in advance ...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

shailesh pratapwar wrote:
I agree with your comment. So, we can just infer that if we have an iterator then this Collections.synchronizedList() would be not useful. what say ?
Also, I wonder if there is any scenario where this Collections.synchronizedList() would be useful. Could you please demonstrate the use of this method with any program so that i could understand it perfectely.


No idea how you got that inference....

1. If you have an iterator that needs to do *a lot* of work (ie. takes a long time to run), it generally needs to have a stable collection to work on anyway. So, you need to do this regardless.

2. If your iterator doesn't actually change the collection, ie. it just needs a snapshot / view, then these types of operations are very short lived. So, doing this is not an issue anyway.

In other words, in my opinion. this should be useful for the majority of cases; either you need to do this regardless, or it is not too big a deal (short lived). The only exception is the rare case, where your application doesn't actually need to change the collection via the iterator (working on a snapshot), and it still has *a lot* of work to do (takes a long time to run). In that case, one option is to consider using a copy-on-write list, such as the java.util.concurrent.CopyOnWriteArrayList.

Henry

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

shailesh pratapwar wrote:I agree with your comment. So, we can just infer that if we have an iterator then this Collections.synchronizedList() would be not useful. what say ?

Not at all. All it means is that you can't use an Iterator while any modifications might conflict. You could argue that they should have provided a weakly consistent iterator (particularly in the case of a LinkedList), but that wasn't really the intent of the class, and I believe it's also stated in the documentation.

Also, I wonder if there is any scenario where this Collections.synchronizedList() would be useful. Could you please demonstrate the use of this method with any program so that i could understand it perfectely.

No, but I can certainly give you an example:
I have a shopping website that is backed by a List (or Map, or Set) of inventory. All updates to that List need to be verified and done in real time, and there may be many people on the system; therefore each update needs to "see" the results of any other. However, I have no particular need to update the display unless a user tells me they want to do so by hitting a "refresh" key, in which case a 'snapshot' of the List will do fine.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection.synchronizedList usage