[ImageJ-devel] DefaultAutoscaleService - Concurrency Issue

Curtis Rueden ctrueden at wisc.edu
Thu Aug 15 10:35:21 CDT 2013


Hi Michael,

> we have a race condition problem using the DefaultAutoscaleService. If
> the methods() method is called from several threads at the same time
> the first invocation will enter buildDataStructures and generate the
> methods object. However the 2nd... invocation can pass the "if
> condition" even if methods has not yet been filled by
> buildDataStructures.

Yes, many of the ImageJ2, SciJava Common and SCIFIO services have been
updated to initialize their data structures only as needed. This has
greatly sped up context creation, at the cost of potential threading issues
and unintuitive code execution order. Services patterned this way are known
to be *not* thread-safe, so your workaround is the short-term solution here.

> Our workaround synchronizes some of our code but it would be more
> efficient if the DefaultAutoscaleService could handle this.

We need to make a decision about whether services patterned this way should
be thread-safe or not. They probably should be, but it's tricky. What I
want to avoid is the negative performance impact of synchronizing when not
completely necessary.

I filed a ticket for this work:
http://trac.imagej.net/ticket/1980

Regards,
Curtis


On Thu, Aug 15, 2013 at 7:25 AM, Michael Zinsmaier <michael.zinsmaier at gmx.de
> wrote:

> Hi Curtis,
>
> we have a race condition problem using the DefaultAutoscaleService. If the
> methods() method is called from several threads at the same
> time the first invocation will enter buildDataStructures and generate the
> methods object. However the 2nd... invocation can pass the "if condition"
> even if methods has not yet been filled by buildDataStructures.
>
> This happens in KNIP if we execute several plugins that rely on this
> service at the same time (and for the first time).
> Our workaround synchronizes some of our code but it would be more
> efficient if the DefaultAutoscaleService could handle this.
>
> Best regards Michael
>
> ------------------------------**------------------------------**
> -----------------
>
> DefaultAutoscaleService
>
>     private void buildDataStructures() {
>         methods = new ConcurrentHashMap<String, AutoscaleMethod>();
>         methodNames = new ArrayList<String>();
>         for (final AutoscaleMethod method : getInstances()) {
>             final String name = method.getInfo().getName();
>             methods.put(name, method);
>             methodNames.add(name);
>         }
>     }
>
>     private Map<? extends String, ? extends AutoscaleMethod> methods() {
>         if (methods == null) buildDataStructures();
>         return methods;
>     }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://imagej.net/pipermail/imagej-devel/attachments/20130815/86586f71/attachment.html>


More information about the ImageJ-devel mailing list