harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Spark Shen <smallsmallor...@gmail.com>
Subject Re: [classlib][luni] Suspicious behavior of java.util.EnumSet
Date Mon, 21 Aug 2006 04:56:27 GMT
Leo Li 写道:
> Hi, Spark:
> Yes, I think it is RI's bug.
> But It should throw ConcurrentModificationException as spec says:
> 1. This exception may be thrown by methods that have detected
> concurrent modification of an object when such modification is not
> permissible.
> 2. Note that this exception does not always indicate that an object has
> been concurrently modified by a *different* thread. If a single thread
> issues a sequence of method invocations that violates the contract of an
> object, the object may throw this exception. For example, if a thread
> modifies a collection directly while it is iterating over the collection
> with a fail-fast iterator, the iterator will throw this exception.
>
> The iterator 's remove() action relies on the result of previous
> next(), but is interrupted by the set.remove() method. I think it is the
> case.
>
> Besides, If the same thing is applied to Hashset:
> public static void main(String[] args) {
> HashSet set = new HashSet();
> Object o = new Object();
> set.add(o);
> Iterator iter = set.iterator();
> iter.next();
> set.remove(o);
> iter.remove();
> }
> It will throw ConcurrentModificationException as expected.:)
>
>
There is a paragraph from spec clearly states that 
ConcurrentModificationException will never be thrown out from the 
iterator returned
by EnumSet. Cited below:
"The returned iterator is /weakly consistent/: it will never throw 
|ConcurrentModificationException| <cid:part1.01020909.06080700@gmail.com>
and it may or may not show the effects of any modifications to the set 
that occur while the iteration is in progress."

Best regards
> On 8/19/06, Spark Shen <smallsmallorgan@gmail.com> wrote:
>>
>> Hi All:
>> The following behavior of RI java.util.EnumSet seems odd. Do you have
>> any opinion on whether it is a bug of RI?
>>
>> import java.util.EnumSet;
>> import java.util.Iterator;
>> public class Test {
>> static enum EnumFoo {
>> a, b,
>> }
>>
>> public static void main(String[] args){
>> EnumSet<EnumFoo> set = EnumSet.noneOf(EnumFoo.class);
>> set.add(EnumFoo.a);
>> Iterator<EnumFoo> iterator = set.iterator();
>> iterator.next();
>>
>> set.remove(EnumFoo.a);
>> iterator.remove(); (1)
>> // The output value is true
>> System.out.println(set.contains(EnumFoo.a));
>> // The output value is 64
>> System.out.println(set.size());
>> }
>> }
>> IMHO, when (1) is executed, an IllegalStateException should be thrown
>> out, since the element EnumFoo.a does not exist at the moment.
>> Any thoughts?
>>
>> Best regards
>>
>> -- 
>> Spark Shen
>> China Software Development Lab, IBM
>>
>>
>> ---------------------------------------------------------------------
>> Terms of use : http://incubator.apache.org/harmony/mailing.html
>> To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
>> For additional commands, e-mail: harmony-dev-help@incubator.apache.org
>>
>>
>
>


-- 
Spark Shen
China Software Development Lab, IBM


---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Mime
View raw message