Who knows??

I think there is at least one problem; If threadA is interrupted
between the lines in returnInstance(), then the 'next' field in the
now 'first' referenced CompositeMethodInstance, will point to.... yes,
what?? Meaning that another thread would take the returned value, but
set the 'first' to a spurious value, and only a thread after that
might be buggered up. Yes, a long shot, and possibly hard to verify or
trigger...



On Thu, Aug 25, 2011 at 3:07 PM, Rickard Öberg <[email protected]> wrote:
> Hi guys,
>
> I'm looking through the caching of composite method instances, and noticed
> that if I use the pool that uses AtomicReferences instead of "synchronized"
> to maintain the pool, the performance goes up by a nr of million invocations
> per second. So, the main question is whether the pool logic is correct. Here
> it is:
> public final class AtomicInstancePool
>    implements InstancePool<CompositeMethodInstance>
> {
>    private final AtomicReference<CompositeMethodInstance> first = new
> AtomicReference<CompositeMethodInstance>();
>
>    public CompositeMethodInstance getInstance()
>    {
>        CompositeMethodInstance firstInstance;
>        do
>        {
>            firstInstance = first.get();
>        } while(firstInstance != null && !first.compareAndSet( firstInstance,
> firstInstance.getNext() ));
>
>        return firstInstance;
>    }
>
>    public void returnInstance( CompositeMethodInstance
> compositeMethodInstance )
>    {
>        CompositeMethodInstance previous = first.getAndSet(
> compositeMethodInstance );
>        compositeMethodInstance.setNext( previous );
>    }
> }
> ---
> The main question is: is the getInstance/returnInstance methods thread-safe
> or not? If I understand the AtomicReference API correctly, I think it is.
> Can you see any way that this could get screwed up?
>
> /Rickard
>
> _______________________________________________
> qi4j-dev mailing list
> [email protected]
> http://lists.ops4j.org/mailman/listinfo/qi4j-dev
>



-- 
Niclas Hedhman, Software Developer
http://www.qi4j.org - New Energy for Java

I live here; http://tinyurl.com/3xugrbk
I work here; http://tinyurl.com/24svnvk
I relax here; http://tinyurl.com/2cgsug

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to