4 Replies Latest reply on Feb 20, 2008 1:07 PM by Ales Justin

    POJO thread safety

    David Lloyd Master

      If I have a POJO that (for example) represents a service that has a few properties that will be injected, what thread-safety precautions must I take? Will the POJO be instantiated, configured, and started/stopped all from the same thread? If not, what guarantees are made with respect to synchronization externally?

        • 1. Re: POJO thread safety
          Ales Justin Master

           

          "david.lloyd@jboss.com" wrote:
          If I have a POJO that (for example) represents a service that has a few properties that will be injected, what thread-safety precautions must I take?

          It depends on your service. :-)
          See what we do below.

          "david.lloyd@jboss.com" wrote:

          Will the POJO be instantiated, configured, and started/stopped all from the same thread?

          Nope or it depends. ;-)
          e.g. first thread will install the pojo, but it has some dependencies that don't allow it to be configured (missing injection)
          Then that missing injection bean gets installed (via HDScanner thread that picked up -beans.xml), which pushes our first bean fwd.
          So threads differ.

          "david.lloyd@jboss.com" wrote:

          If not, what guarantees are made with respect to synchronization externally?

          We are locking parts of the Controller code that change states.
          So if one thread moves context fwd, the next thread will wait, and then see that this was already moved.
          Meaning your pojo will only go through states exactly once.

          • 2. Re: POJO thread safety
            David Lloyd Master

            OK, that makes sense. Thanks.

            • 3. Re: POJO thread safety
              David Lloyd Master

              Actually one more question - are the locks held when doing auto-wiring and injection and that sort of thing? In other words, if I have beans A and B, and I want a property from A to be injected into B, will the same lock be held that is used to change states during the injection? I'm concerned more about memory visibility than locking here.

              • 4. Re: POJO thread safety
                Ales Justin Master

                 

                "david.lloyd@jboss.com" wrote:
                Actually one more question - are the locks held when doing auto-wiring and injection and that sort of thing?

                Yup, a read lock.

                "david.lloyd@jboss.com" wrote:

                In other words, if I have beans A and B, and I want a property from A to be injected into B, will the same lock be held that is used to change states during the injection? I'm concerned more about memory visibility than locking here.

                Nope.
                We try to unlock/re-lock things as much as possible, to not keep things in waiting for unnecessary reasons.
                But we do make sure things are consistent, applying write/read locks where needed.
                See AbstractController.incrementState for such unlocking usage.