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)? Thanks so much
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
If thread A calls synchronized method M1 which in turn calls unsynchronized method M2, then thread B can still call M2 without blocking.
Synchronized method acquires and releases intrinsic lock on the object on which it is called. This is why it may block. Unsynchronized method doesn't attempt to acquire any lock (unless it is done explicitly in the code).
Thus, if you need to ensure mutual exclusion for M2 as well, you should make it synchronized regardless of whether its callers (like M1) are synchronized or not.
The lock belongs to the thread, not to the method (or more precisely, its stack frame). It just so happens that if you have a synchronized method, you're guaranteed that the thread will own the lock before the body of the method start, and will release it afterwards.
Another thread can still invoke the second, non-synchronized method. An unsynchronized method can be called by any thread at any time.
The lock doesn't belong to the thread. The lock actually belongs to the object(or Class in case of Class level lock), and a thread acquires lock on the Object(or Class in case of Class level lock) within a synchronized context. Now, there is no lock propagation in java as it is discussed above. Here is a small demo:
public class TestThread {
}
public class ThreadCreator1 implements Runnable {
public class Task {
}
public class Task2 {
}
Just I have used Reentrant lock here. If the above code is run, then there will be interleaving between thread 1 and thread 3, but if the lock portion of Task2 class is uncommented, then there will be no interleaving and the thread which acquire the lock first will complete fully first, then it will release the lock and then the other thread can carry on.
Yes and no.
If you are in a
synchronized
method, then calls to other methods that are alsosynchronized
by other threads are locked. However calls to non-synchronized methods by other threads are not locked -- anyone can call them at the same time.Also, if you call
someSynchronizedMethod()
but happen to be within thesomeNonSynchronizedMethod()
method, you still hold the lock. The lock is enabled when you enter a synchronized 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:
Yes. Other methods can access non-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 theClass
object in theClassLoader
.If the method is an instance method then the lock is on the instance of the class.
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 samesynchronized
method at the same time with different instances. But if 2 threads try to operate onsynchronized
methods on the same instance, one will block until the other one exits the method.