Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method

In Java, if a synchronized method contains a call to a non-synchronized, can another method still access the non-synchronized method at the same time? Basically what I'm asking is everything in the synchronized method have a lock on it (including calls to other synchronized methods)?

like image 799
dido Avatar asked Mar 01 '12 23:03

dido


People also ask

Can a synchronized method call another synchronized method?

As an answer to what I would guess is the actual question: yes, the synchronized keyword uses recursive locks; you can safely call a synchronized method from another synchronized method.

Is lock required for synchronized method?

If a thread wants to execute a synchronized method on a given object, first it has to get a lock of that object. Once thread got the lock then it is allowed to execute any synchronized method on that object.

Does synchronized method lock object?

When a method is declared as synchronized; the thread holds the monitor or lock object for that method's object. If another thread is executing the synchronized method, your thread is blocked until that thread releases the monitor.

Can a thread call a non synchronized instance method of an object when a synchronized method is being executed?

To answer your question. Yes, a Non synchronized method can always be called without any problem.


1 Answers

If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method

The answer depends on the context.

If you are in a synchronized method for an object, then calls by other threads to other methods of the same object instance that are also synchronized are locked. However calls by other threads to non-synchronized methods are not locked – anyone can call them at the same time.

public synchronized void someSynchronizedMethod() {     ...     someNonSynchronizedMethod();     ... }  // anyone can call this method even if the someSynchronizedMethod() method has // been called and the lock has been locked public void someNonSynchronizedMethod() {    ... } 

Also, if you call someSynchronizedMethod() but happen to be within the someNonSynchronizedMethod() method, you still hold the lock. The lock is enabled when you enter a synchronized method (or block) and is disabled when you exit that method. You can call all sorts of other unsynchronized methods and they will still be locked.

But you are asking two different things in your question:

In Java, if a synchronized method contains a call to a non-synchronized, can another method still access the non-synchronized method at the same time?

Yes. Other methods can access non-synchronized methods.

Basically what I'm asking is everything in the synchronized method have a lock on it (including calls to other synchronized methods)?

Uh, yes. Other calls to synchronized methods are locked. But non-synchronized methods are not locked.

Also, remember that if the method is static then the lock is on the Class object in the ClassLoader.

// this locks on the Class object in the ClassLoader public static synchronized void someStaticMethod() { 

If the method is an instance method then the lock is on the instance of the class.

// this locks on the instance object that contains the method public synchronized void someInstanceMethod() { 

There are 2 different locks in those 2 cases.

Lastly, when you are dealing with synchronized instance methods, each instance of the class is what is locked. This means that two threads could be in the same synchronized method at the same time with different instances. But if 2 threads try to operate on synchronized methods on the same instance, one will block until the other one exits the method.

like image 131
Gray Avatar answered Oct 12 '22 00:10

Gray