Test your might

Suppose I build a collection and all the elements I add are synchronized, is there ever a way I could have an issue with multiple threads?
[145 byte] By [tjacobs01a] at [2007-11-26 17:40:29]
# 1
Are the elements synchronized or the collection?
zadoka at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 2
> Are the elements synchronizedWhat do you mean?or the collection?Sure. The collection is synchronized.
tjacobs01a at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 3
> is there ever a way I could> have an issue with multiple threads?Sure, if only the collection is synchronized and the elements are mutable. Depends on what you think of as "issue".
stefan.schulza at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 4

> > Are the elements synchronized

>

> What do you mean?

>

> or the collection?

>

> Sure. The collection is synchronized.

Just trying to understand the context of your question. I didn't know what you meant by the elements are synchronized? Also don't know what you mean by issues? What are you attempting to do with it?

zadoka at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 5

> mean by issues? What are you attempting to do with

> it?

Just trying to raise a little discussion about all the ways you could still have threading issues

> question. I didn't know what you meant by the

> elements are synchronized? Also don't know what you

no elements aren't synchronized. Just the collection

tjacobs01a at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 6

Sure, there are lots of ways you could have threading problems. Even with simple code likeif (!collection.contains(thing)) {

collection.add(thing);

}

it isn't sufficient to synchronize the call to contains() and the call to add() separately. Which is what you are doing.

DrClapa at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 7
> Suppose I build a collection and all the elements I> add are synchronized, is there ever a way I could> have an issue with multiple threads?only if the elemnts added are not immutable? synchronized is a lil different from thread safe I guess.
kilyasa at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 8

> only if the elemnts added are not immutable?

> synchronized is a lil different from thread safe I

> guess.

Even if you had immutable objects you could still have issues

consider the equals method. First off, equals is defined in Object as not synchonized, so your equals method probably isn't synchronized. BUT even if it was, or even if you synchronized on it. But then the list you're comparing to might change, so you synchronize on that... even then you could STILL have thread race conditions if that other list wasn't synchronized

... Or you could just make your own collection where the underlying data structure is a public member variable :)

tjacobs01a at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 9

> > only if the elemnts added are not immutable?

> > synchronized is a lil different from thread safe I

> > guess.

>

> Even if you had immutable objects you could still

> have issues

>

> consider the equals method. First off, equals is

> defined in Object as not synchonized, so your equals

> method probably isn't synchronized.

well neither is hashcode, but they dont change the state of the object, plus for immutable objects you cant even have the state changed so how can it be an issue, please explain.

kilyasa at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 10
sorry I thought it was a thread safe collection... being added with immutable object
kilyasa at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 11

What I'm saying is that even if you try to make your list as threadsafe as possible, there is no way to make equals totally threadsafe because things could be changing with the object you are comparing to. Not only that, but the other object would have to be totally synchronized aroujnd its data AND synchronize on the list being compared to in equals to stand a chance of total thread safety.

My head hearts. I'm going to get a beer.

tjacobs01a at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...
# 12

> What I'm saying is that even if you try to make your

> list as threadsafe as possible, there is no way to

> make equals totally threadsafe because things could

> be changing with the object you are comparing to. Not

Not if the object is immutable.

>

> My head hearts. I'm going to get a beer.

kilyasa at 2007-7-9 0:08:39 > top of Java-index,Core,Core APIs...