AssistedInject and lock contention

17 views
Skip to first unread message

Karthik Krishnan

unread,
Nov 3, 2009, 4:38:47 PM11/3/09
to google-guice
Hi All,

I am using AssistedInject to inject Strings with variable values into
an object. I am using MapBinder to map the implementation to the type
of instance. We are able to get an instance of the class with the
injected string value. On an off chance, I looked at issue 435 that
mentions lock contention http://code.google.com/p/google-guice/issues/detail?id=435
with assisted inject.

We would not want the issue cropping up in our application. Is there a
work around or an emergency patch we should incorporate in our
application.

limpb...@gmail.com

unread,
Nov 3, 2009, 9:56:10 PM11/3/09
to google-guice
We're working on this. For now, use the old form of assisted inject
with the "@AssistedInject" annotation rather than the "@Inject"
annotation.

On Nov 3, 1:38 pm, Karthik Krishnan <krishnan.1...@gmail.com> wrote:
> Hi All,
>
> I am using AssistedInject to inject Strings with variable values into
> an object. I am using MapBinder to map the implementation to the type
> of instance. We are able to get an instance of the class with the
> injected string value. On an off chance, I looked at issue 435 that
> mentions lock contentionhttp://code.google.com/p/google-guice/issues/detail?id=435

Dhanji R. Prasanna

unread,
Nov 3, 2009, 9:59:23 PM11/3/09
to google...@googlegroups.com
I wouldn't worry about this unless you are *actually* seeing the lock contention problem I describe.

Furthermore, if you are mainly using assisted inject for long lived objects you will be fine. =)

Dhanji.

Karthik Krishnan

unread,
Nov 4, 2009, 3:09:41 AM11/4/09
to google...@googlegroups.com
I am not sure how to reproduce the lock contention. I used this code sample as a reference http://pastie.org/453944 to build my own map binder. Would this work?

Karthik Krishnan

unread,
Nov 4, 2009, 3:45:41 AM11/4/09
to google...@googlegroups.com
I should have included my code in my previous post

My module:
@Override
    protected void configure() {
        MapBinder<String, DelegateFactory> mapBinder =
            MapBinder.newMapBinder(binder(), String.class,
                    DelegateFactory.class);
        // Implementation that is responsible for fetching the three tiered
        // client type.
        mapBinder.addBinding("CLIENT_TYPE") // Constructor key attribute
                .toProvider(FactoryProvider.newFactory(
                        DelegateFactory.class,
                        Delegate.class));
        // Implementation that is responsible for fetching the credit product
        // type.
        mapBinder.addBinding("CP_TYPE")
                .toProvider(FactoryProvider.newFactory(
                        DelegateFactory.class,
                        Delegate.class));
        // Implementation that is responsible for fetching the report type.
        mapBinder.addBinding("REPORT_TYPE")
                .toProvider(FactoryProvider.newFactory(
                        DelegateFactory.class,
                        Delegate.class));
    }

Constructor

@Inject
public Delegate(@Assisted String key) {
    this.key = key;
}

@Inject
 public void setFactory(Map<String, DelegateFactory> factory) {
        this.factory = factory;
        this.delegate = factory.get("CLIENT_TYPE").create("CLIENT_TYPE");
 }

Invocation

Injector injector = Guice.createInjector(new DelegateModule());
injector.injectMembers(this);

Dhanji R. Prasanna

unread,
Nov 4, 2009, 5:23:35 AM11/4/09
to google...@googlegroups.com
The solution is quite simple. Ask the following questions:

- Do you use assisted inject (the new version) in a critical code path to instantiate object hundreds of times a minute?
- Do you use assisted inject to create objects every now and then, or as singletons?

If you answered:

no, yes, no no, you are fine.

If you answered yes, no, then you have a problem. But you should verify with profiling tools before you believe you are actually seeing this issue. Otherwise as jesse says, the simple solution is use the @AssistedInject annotation for assisted inject objects (instead of just @Inject).

Dhanji.
Reply all
Reply to author
Forward
0 new messages