Dan
Dan

Reputation: 2174

java: running thread methods in the same class

I have three threads on the same class i.e Thread A obj, Thread B obj, Thread C obj and that class contains 3 static synchronized methods so when we start the 3 threadssaya.meth1, b.meth2, c.meth3what will happen – does all three will execute or only one ?`

Update: interviewr asked me this question so actually i do have any code to write it here

Upvotes: 1

Views: 275

Answers (5)

Extreme Coders
Extreme Coders

Reputation: 3511

The primary difference between static synchronized methods and non static synchronized methodsis that non-static synchronized methods hold a lock on the instance of the class whereas static synchronized methods lock on the class object.

Since in java there exists one class object per class, only one thread can execute inside a static synchronized method in the same class.

For non-static synchronized methods only one thread can execute inside a static synchronized method in the same object

And one more point is that synchronization is at object level not as method level.

Upvotes: 0

ErstwhileIII
ErstwhileIII

Reputation: 4843

If execution for objects of the same class attempt to enter the same synchronized method (whether static or not)from different threads, they will be synchronized (i.e. execute one at a time).

Upvotes: 1

Sanjeev
Sanjeev

Reputation: 9946

then they will execute it one by one in serial manner as the methods are static synchronized. So the lock will be held at Class Level not the method level. Hence one thread will acquire lock and others will have to wait.

You should try running this and see.

Upvotes: 2

TheLostMind
TheLostMind

Reputation: 36304

The methods are static and synchronized.. So, the lock will be on the class object.. Not on the instances of the class.. So the methods will run one after another.... Thread2 and Thread3 will have to wait until thread1 completes method1().. syncronization is NOT at method level.. it is always at object level... beit instance object or class object.

Upvotes: 5

Niels Bech Nielsen
Niels Bech Nielsen

Reputation: 4859

Once you invoke a synchronized method, the VM will automatically ask a grant of access for the object on which you are invoking the method. If that is given it will enter the synchronized method. Upon exit til will release this access right. While holding the rights, no-one else is allowed into any synchronized method on the same object, effectively serializing the requests.

does that make sense?

Synchronization is designed to make things thread-safe and avoid race conditions, and this fashion reduce access to at most one thread. There is no way for the program to find out whether two synchronized methods A and B are otherwise connected, so it has the policy of least tolerance.

If you have more synchronization that necessary, e.g. that A and B needs to be mutually exclusive and C and D needs to be mutually exclusive, but not between, say A and C then the advice is typically to modularise your code so A+B goes into one object while C+D goes into another, hence avoiding to step over each others toes

Upvotes: 1

Related Questions