• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with Multithreading and Collections  RSS feed

 
Steffen Reinhard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Synchronize on the list object, and use a synchronized list wrapper:

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

You need to do both.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the error only occurs when your using interators I believe. You have not specified any iterator use!?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!