[cdi-dev] Thread safety of SeContainer and SeContainerInitializer

Martin Kouba mkouba at redhat.com
Fri Jan 6 02:39:07 EST 2017


Dne 5.1.2017 v 22:56 Laird Nelson napsal(a):
> (Resurrecting this thread.  Happy New Year!)
>
> Another (related) case I had a question about.
>
> 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?  It sounds like strictly speaking no, I may not—i.e.
> that the specification, since it says nothing one way or the other about
> threading, therefore implies that only one thread may, say, iterate over
> the container (either using its iterator() or stream() methods).

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 ;-)

>
> On the other hand, I see no restriction in the specification on the
> creation of threads from, say, a managed bean, which means it is always
> possible that at any point an Instance object may be used by many
> threads.

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 and if you do, only dependent and application context 
will work as usual.

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

> Or, further, since events may be fired asynchronously, then it
> would /seem/ to follow that iteration over an Instance must be thread safe.
>
> 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+.

>
> Best,
> Laird
>
> On Thu, Dec 8, 2016 at 1:37 AM Martin Kouba <mkouba at redhat.com
> <mailto:mkouba at redhat.com>> wrote:
>
>     I agree with John, it's not guaranteed. However, the original intention
>     (based on Weld SE impl) was:
>
>     * SeContainerInitializer is not thread-safe and should not be shared
>     between threads
>     * it should be safe to call SeContainer.close() from any thread
>
>     Martin
>
>     Dne 7.12.2016 v 22:01 John Ament napsal(a):
>     > I would say that this is not guaranteed.
>     >
>     >
>     >
>     >
>     ------------------------------------------------------------------------
>     > *From:* cdi-dev-bounces at lists.jboss.org
>     <mailto:cdi-dev-bounces at lists.jboss.org>
>     > <cdi-dev-bounces at lists.jboss.org
>     <mailto:cdi-dev-bounces at lists.jboss.org>> on behalf of Laird Nelson
>     > <ljnelson at gmail.com <mailto:ljnelson at gmail.com>>
>     > *Sent:* Wednesday, December 7, 2016 3:56 PM
>     > *To:* cdi-dev at lists.jboss.org <mailto:cdi-dev at lists.jboss.org>
>     > *Subject:* [cdi-dev] Thread safety of SeContainer and
>     > SeContainerInitializer
>     >
>     > Are SeContainer and SeContainerInitializer safe for concurrent use by
>     > multiple threads?  The string "concurren" shows up only four times in
>     > the 2.0 specification but not in this context.
>     >
>     > I'm most interested in whether it is guaranteed that one thread
>     may call
>     > SeContainer::initialize and another may call SeContainer::close.
>     >
>     > I'm assuming that this is /not/ guaranteed.
>     >
>     > Best,
>     > Laird
>     >
>     ------------------------------------------------------------------------
>     > NOTICE: This e-mail message and any attachments may contain
>     > confidential, proprietary, and/or privileged information which
>     should be
>     > treated accordingly. If you are not the intended recipient, please
>     > notify the sender immediately by return e-mail, delete this
>     message, and
>     > destroy all physical and electronic copies. Thank you.
>     >
>     >
>     > _______________________________________________
>     > cdi-dev mailing list
>     > cdi-dev at lists.jboss.org <mailto:cdi-dev at lists.jboss.org>
>     > https://lists.jboss.org/mailman/listinfo/cdi-dev
>     >
>     > Note that for all code provided on this list, the provider
>     licenses the code under the Apache License, Version 2
>     (http://www.apache.org/licenses/LICENSE-2.0.html). For all other
>     ideas provided on this list, the provider waives all patent and
>     other intellectual property rights inherent in such information.
>     >
>
>     --
>     Martin Kouba
>     Software Engineer
>     Red Hat, Czech Republic
>


More information about the cdi-dev mailing list