[concurrency-interest] Impossible Exception?

Thomas Hawtin Thomas.Hawtin at Sun.COM
Mon Jan 14 08:56:31 EST 2008

Sam Berlin wrote:
>     /** Retrieves the reference, creating it if necessary. */
>     public synchronized T get() {
>         if(obj == null) {
>             if(constructing)
>                 throw new IllegalStateException("constructing again!");
>             constructing = true;
>             obj = createObject();
>         }
>         return obj;
>     }

In addition to exceptions from createObject there is another way that it 
is possible for the IllegalStateException to be thrown. because the lock 
is reentrant, createObject could call get. Alternatively createObject 
could wait on the lock, and another thread could then enter the method. 
More "unlikely", a subclass could implement Cloneable, createObject or 
another thread could then clone.

Tom Hawtin

More information about the Concurrency-interest mailing list