1

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

2
  • 2
    The question will become a lot more clear if you post some relevant code. Commented Feb 19, 2014 at 6:19
  • @ExtremeCoders my interviewer asked me this question orally. Commented Feb 19, 2014 at 6:21

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, i also said to interview this same thing
@SotiriosDelimanolis - A bad habit... which BTW I intend to end... :)
2

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.

4 Comments

The methods are static and synchronized.
They 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...
@SotiriosDelimanolis i oversight the static .. my mistake. updated the answer. thanks for pointing out.
@SotiriosDelimanolis just updated. Hope it clarifies better now.
1

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

1 Comment

Sorry, didn't see the reference to static method, goes with Class as everybody else says too
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.