[concurrency-interest] LinkedBlockingQueue does not throwNullPointerException for the method call contains

Jason Mehrens jason_mehrens at hotmail.com
Tue Apr 17 14:37:46 EDT 2007


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


>From: "Joe Bowbeer" <joe.bowbeer at gmail.com>
>To: concurrency-interest <concurrency-interest at cs.oswego.edu>
>Subject: Re: [concurrency-interest] LinkedBlockingQueue does not 
>throwNullPointerException for the method call contains
>Date: Mon, 16 Apr 2007 16:19:13 -0700
>
>Martin Buchholz mentioned off-list that we could link the instances of
>"(optional)" in the Collection method descriptions to the paragraph in
>the Collection class description that covers optional restrictions.
>
>Is this worthwhile?
>
>Here's what the fix would look like:
>
>Collection.java
>
>1. Add "optional-restrictions" anchor.
>
>  * <p><a name="optional-restrictions"/>
>  * Some collection implementations have restrictions on the elements that 
>they
>  * may contain.  For example, some implementations prohibit null elements,
>
>2. Add links to the optional restrictions paragraph.
>
>There are ten instances of "(optional)" -- resulting from five sets of
>ClassCastException and NullPointerException.  In each instance,
>"(optional)" would be replaced by:
>
>  (<a href="#optional-restrictions">optional</a>)
>
>--Joe
>_______________________________________________
>Concurrency-interest mailing list
>Concurrency-interest at altair.cs.oswego.edu
>http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

_________________________________________________________________
The average US Credit Score is 675. The cost to see yours: $0 by Experian. 
http://www.freecreditreport.com/pm/default.aspx?sc=660600&bcd=EMAILFOOTERAVERAGE



More information about the Concurrency-interest mailing list