[concurrency-interest] Concurrency-interest Digest, Vol 27, Issue 35

Martin Buchholz Martin.Buchholz at Sun.COM
Wed Apr 18 17:12:44 EDT 2007


Collection.contains and Collection.remove are both
spec'ed in the same way to use the phrase "(optional)",
but they are not tied to each other.  Sensible implementations
will likely throw the same exceptions for unsuitable elements,
but there is no guarantee, and it is strictly speaking an
incompatible spec change, and it doesn't seem worth adding
such a guarantee at this time.  (All changes to the Java spec
require a lot of paperwork!)

Martin

> From: "Jason Mehrens" <jason_mehrens at hotmail.com>
> Subject: Re: [concurrency-interest] LinkedBlockingQueue does not
> 	throwNullPointerException for the method call contains
> To: joe.bowbeer at gmail.com, concurrency-interest at cs.oswego.edu
> Message-ID: <BAY142-F285177B4C26B3B25993D3B83510 at phx.gbl>
> Content-Type: text/plain; format=flowed
> 
> I think it makes sense to tie the optional behavior of contains to the 
> optional behavior of remove.  From what I can tell that detail is not 
> specified in the docs but, the concrete implementations in the JDK seem to 
> enforce this detail.
> 
> For instance, if I wrote a Collection that was a ReadWriteLock wrapper for a 
> single thread collection like the synchronizedCollection but mapped writes 
> to the write lock and reads to the read lock.
> If remove is written like:
> 
> public boolean remove(Object o) {
>   if(write.tryLock()) {
>     try {
>        return col.remove(o);
>     }
>     finally {
>        write.unlock();
>     }
>   }
> 
>   read.lock();
>   try {
>     if(!col.contains(o)) {
>       return false;
>     }
>   }
>   finally {
>     read.unlock();
>   }
> 
>   write.lock();
>    try {
>       return col.remove(o);
>    }
>    finally {
>       write.unlock();
>    }
> }
> 
> If contains and remove do not handle optional exceptions then same way then 
> "optional" means "depending of the state of write lock" for the above 
> implementation.  Which I don't think was the intended meaning "optional".  
> Is this detail worth specifying?
> 
> Jason Mehrens


More information about the Concurrency-interest mailing list