This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Problems with Multithreading and Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Problems with Multithreading and Collections" Watch "Problems with Multithreading and Collections" New topic
Author

Problems with Multithreading and Collections

Steffen Reinhard
Greenhorn

Joined: Jun 13, 2005
Posts: 19
Hi, all!

On several occasions I tried to access a java.util.LinkedList ll using two threads to write data to the beginning (using addFirst()) while trying to extract data from the end (using removeLast()) thus creating something like a buffer for sequential data access.

Alas! In a better world it would all have worked out the way I planned it.

In reality I keep getting error messages stating that a ConcurrentModificationException has occurred ... Weeeellll ... There's no way I can tell my Java environment that this is exactly what I intended to do: concurrent modification of data using one thread for input and one for output, respectively.

Tried synchronizing on the LinkedList object. Doesn't help. Java seems to be somewhat stubborn when it comes to bypassing the fail-fast behavior of it's Container classes ...

Any ideas, anyone?


Thanks in advance!

Cheers -

Steffen

[ July 20, 2005: Message edited by: Steffen Reinhard ]
[ July 20, 2005: Message edited by: Steffen Reinhard ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

Synchronize on the list object, and use a synchronized list wrapper:

List myList = Collections.synchronizedList(new LinkedList());

You need to do both.


[Jess in Action][AskingGoodQuestions]
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

But the error only occurs when your using interators I believe. You have not specified any iterator use!?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I donot think it is just because of iterator usage.

By the way, what are you doing actually?
notify on the list in the actual processing thread of control,
and wait() in the other...

Then even you will not need synchronized(list_Object) at all.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Problems with Multithreading and Collections
 
Similar Threads
unsupported major.minor version 49.0
listing files in a jar file
main ( String[] args) question
Problem with GUI
Please clarify, if possible.