Umesh Kacha
Umesh Kacha

Reputation: 13686

Java Synchronized and static synchronized method used by Multiple Threads

I get confused with the following scenario all the time

There is only one object of MyClass and there are two threads T1, T2. Now one thread say T2 will be able to use the synchronized method m1() which is having the only MyClass object lock and other thread T2 will get blocked if it will try to access m1().

Now my perception is that if T2 will try to access static synchronized method m2() by accessing static shared field it will get blocked as current object lock is with T1 and wont be able to execute m2() and if there were two objects of Myclass then T2 thread would be able to access m1(). Am I correct or wrong?

class MyClass
{

            public static int i = 5;

        public synchronized void m1()
        {
                System.out.println(i); //uses static field i of MyClass
            //T1 is executing this method
        }

            public static synchronized void m3()
            {
                //T2 will be able to call this method on same object lock while it is using
                //static field i???
                System.out.println(i);//uses static field i of MyClass
            }
}

This is very confusing please help. Thanks in advance.

Upvotes: 1

Views: 1264

Answers (4)

PermGenError
PermGenError

Reputation: 46438

Thread acquires a lock when it enters a synchronized method /block. as m2() is not synchronized, it will be executed by thread 2. remember that, Locks only come into picture when there are synchronized methods

Upvotes: 1

biziclop
biziclop

Reputation: 49814

You're wrong. Objects aren't "locked" by synchronized blocks, the only thing that happens is that other synchronized blocks trying to acquire the monitor for that object have to wait.

To sum it up: synchronized has absolutely no blocking effect on non-synchronized code.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1504122

Now my perception is that if T2 will try to access non synchronized method m2() it will get blocked as current object lock is with T1

No. m2() isn't synchronized, therefore there's nothing to block it. That's the difference between a synchronized and a non-synchronized method.

When a thread tries to enter a synchronized method, it will block until it can acquire the appropriate monitor (lock). It will release the lock when it exits the method. Neither of those steps occurs for a non-synchronized method.

(As an aside, I would personally suggest not using synchronized methods at all, for the most part. Instead, synchronize within the method, on a reference which is only known about within that class. That makes it easier to reason about the code, as only your class will be able to acquire that lock.)

Upvotes: 6

John B
John B

Reputation: 32969

Not correct. Since m2 is not synchronized no thread will be blocked when attempting to invoke it no matter the state of the synchronized method m1.

Acquiring a lock on an object doesn't block access to that object, it just block the ability for another thread to acquire the lock on the same object concurrently. If the other thread does not attempt to acquire the lock, it will not be blocked.

Upvotes: 6

Related Questions