[cdi-dev] Thread safety of SeContainer and SeContainerInitializer

Laird Nelson ljnelson at gmail.com
Fri Jan 6 02:50:00 EST 2017

On Thu, Jan 5, 2017 at 11:39 PM Martin Kouba <mkouba at redhat.com> wrote:

> Dne 5.1.2017 v 22:56 Laird Nelson napsal(a):
> > Suppose I have the standard SeContainer "block" like this:
> >
> > try (final SeContainer container = initializer.initialize()) {
> >   // stuff goes here
> > }
> >
> > Inside that block, may I start threads and have them do things with the
> > container variable?

> I believe you can. It should be safe to use an initialized container
> instance from multiple threads. The spec does not explicitly mention
> this but it can't cover every possibility ;-)

Sure; that's fair.  I guess…maybe there's a blanket statement that could be
added to the specification?  Something that indicates that *unless
otherwise indicated*, core CDI objects (not sure offhand what these are,
but things like BeanManager, CDI, Instance, Context, etc.) used in a Java
SE context are safe for concurrent use by multiple threads.

To state the obvious, I'm not a fan of "try it; see if it works", since I
don't know what any given implementation is going to do.

> You can create a new thread from within a managed bean. BUT there are
> few snags you should be aware of. I.e. in Java EE you should not create
> your own threads

Sure; of course not.

> and if you do, only dependent and application context
> will work as usual.


> See also http://weld.cdi-spec.org/documentation/#0


> > Should the specification include language related to concurrency in
> > these matters and others?
> I think that would require a great deal of effort. But feel free to
> create a spec issue for CDI 2.1+.

OK.  A blanket statement or two, so that exceptional cases can then be
documented on a case-by-case basis would seem to be the way to go here.
I'll file a spec issue.  Thanks for your time.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/cdi-dev/attachments/20170106/14baf70e/attachment.html 

More information about the cdi-dev mailing list