File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection.synchronizedList usage

 
joni novhia
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic