如果一个同步方法调用另一个非同步方法,非同步方法是否有锁 [英] If a synchronized method calls another non-synchronized method, is there a lock on the non-synchronized method

查看:32
本文介绍了如果一个同步方法调用另一个非同步方法,非同步方法是否有锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java 中,如果同步方法包含对非同步方法的调用,那么另一个方法是否仍然可以同时访问非同步方法?基本上我要问的是同步方法中的所有内容都对其进行了锁定(包括对其他同步方法的调用)?

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

推荐答案

如果一个同步方法调用了另一个非同步方法,那么非同步方法是否有锁

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

答案取决于上下文.

如果你在一个对象的synchronized 方法中,那么其他线程对同样synchronized 的同一对象实例的其他方法的调用将被锁定.然而,其他线程对非同步方法的调用被锁定——任何人都可以同时调用它们.

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() {
   ...
}

此外,如果您调用 someSynchronizedMethod() 但碰巧在 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:

在 Java 中,如果同步方法包含对非同步方法的调用,那么另一个方法是否仍然可以同时访问非同步方法?

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.

另外,请记住,如果方法是static,那么锁位于ClassLoader 中的Class 对象上.

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.

最后,当您处理同步实例方法时,类的每个实例都是被锁定的.这意味着两个线程可以在同一个同步方法中同时使用不同的实例.但是如果 2 个线程尝试在同一个实例上操作 synchronized 方法,一个将阻塞,直到另一个退出该方法.

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.

这篇关于如果一个同步方法调用另一个非同步方法,非同步方法是否有锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆