6 Replies Latest reply: May 30, 2008 1:57 AM by 3004 RSS

    static synchronized methods VS non-static synchronized methods ??

    807601
      what is the difference between static synchronized methods and non-static synchronized methods as far as the behavior of the threads is concerned? if a thread is in static synchronized method can another thread access simple (ie. non static) synchronized methods?
        • 1. Re: static synchronized methods VS non-static synchronized methods ??
          3004
          Synchronization is always the same, "as far as the behavior of the threads is concerned." Declaring a method synchronized is just shorthand for what you could do by explicitly syncing a block of code on a particular object.*
          class Foo {
            synchronized void bar() {
              // body
            }
            
            // is the same as
          
            void bar() {
              synchronized (this) {
                // body
              }
            }
          
          
            // and
          
          
            static synchronized qux() {
              // body
            }
          
            // is the same as 
          
            static qux() {
              synchronized (Foo.class) {
                // body
              }
            }
          }
          In all cases, you're just syncing on an object, and which object doesn't matter, except to other methods or blocks that sync on the same object. Everything else is identical, "as far as the behavior of the threads is concerned".

          * There's a slight difference in the bytecode and VM details, but it's not relevant here.
          • 2. Re: static synchronized methods VS non-static synchronized methods ??
            807601
            so that would mean that two thread concurrently execute if one of them access static synchronized method and the other one non-static synchronized method...right?
            • 3. Re: static synchronized methods VS non-static synchronized methods ??
              3004
              javanewbie80 wrote:
              if a thread is in static synchronized method can another thread access simple (ie. non static) synchronized methods?
              You're making synchronization more complex than it is. In terms of exclusivity of access all that syncing does it:

              1) Wait until no other thread holds a particular lock (associated with a explicitly or implicitly specified object).

              2) Execute the code in the synced block or method.

              3) Release the lock.

              The object whose lock we obtain is specified explicitly by the reference we provide in synchronized (something), or implicitly as this or the class object, as shown in my previous post.

              So whether a given thread can execute a method or block of code is determined solely by whether some other thread holds the lock that is needed to execute that block or method.
              • 4. Re: static synchronized methods VS non-static synchronized methods ??
                3004
                javanewbie80 wrote:
                so that would mean that two thread concurrently execute if one of them access static synchronized method and the other one non-static synchronized method...right?
                Correct. One holds the lock for an instance of the class, and the other holds the lock for the class' Class object.

                And two threads can concurrently execute any synced method and any non-synced method.

                And two threads can concurrently execute a non-static synced method if one thread is executing it on one istance of the class and the other on another instance--two distinct object, two distinct locks.
                • 5. Re: static synchronized methods VS non-static synchronized methods ??
                  807601
                  Great. Thanks. This whole explanation made a lot of sense to me. Probably I was just trying to complicate things unnecessarily.
                  • 6. Re: static synchronized methods VS non-static synchronized methods ??
                    3004
                    javanewbie80 wrote:
                    Great. Thanks. This whole explanation made a lot of sense to me.
                    Cool, glad I was able to help!
                    Probably I was just trying to complicate things unnecessarily.
                    It's a classic case of complexity inversion. It seems simpler to say something like "synchronization locks the class" or "...locks the method" than to give my explanation and then extrapolate the implications. Just like the seemingly simpler, but incorrect, "Java passes objects by reference," vs. the correct "Java passes references by value," or Java's seemingly complex I/O vs. other languages' int x = readInt(); or whatever.

                    In the seemingly complex case, the primitive construct is simpler, but the higher level construct requires more assembly or derivation of the primitive constructs, making that case seem more complicated.

                    Okay, I just re-read that, and it seems like I'm making no sense, but I'll leave it, just in case somebody can get some meaning out of it. :-)