类中同步方法组合的线程行为

发布于 2024-10-08 03:44:34 字数 1263 浏览 1 评论 0原文

情况 1

我有 2 个同步方法,如下所示:

class A {
    public void synchronized methodA() {}
    public void synchronized methodB() {}
}

A: 我有线程 T1 和 T2。线程能否同时执行(分别)属于 A 类同一个实例的methodAmethodB

  • 我的分析:答案是否定的,因为线程T1只会执行一个方法,线程T2将被阻塞,直到T1执行完毕。

B:我有线程 T1 和 T2。线程能否同时执行(分别)属于 A 类不同实例的 methodAmethodB

  • 我的分析:答案是,因为T1和T2可以执行属于A类不同实例的methodAmethodB并且不会被阻塞。

根据我对案例 1 的分析,我的理解是否正确?

更新:情况 2

我有 2 个同步方法,一个是非静态的,另一个是静态的。

class A {        
    public void synchronized methodA() {}    
    public void static synchronized methodB() {}    
}       

A:我有线程 T1 和 T2。线程能否同时执行(分别)属于类 A 的同一个实例的methodAmethodB

  • 我的分析:答案是否定的,因为T1只会执行一个方法,T2将被阻塞,直到T1执行完毕。

B:我有线程 T1 和 T2。线程能否同时执行(分别)属于 A 类不同实例的methodAmethodB

  • 我的分析:答案是,因为T1和T2可以执行属于不同实例的methodAmethodB,并且不会被阻塞。

根据我对案例2的分析,我的理解是否正确?

Case 1

I have 2 synchronized methods as shown below:

class A {
    public void synchronized methodA() {}
    public void synchronized methodB() {}
}

A: I have threads T1 and T2. Can the threads simultaneously executemethodA and methodB (respectively) belonging to the same instance of class A?

  • My analysis: The answer is no because only one method will be executed by thread T1 and thread T2 will be blocked until T1 finishes execution.

B: I have threads T1 and T2. Can the threads simultaneously execute methodA and methodB (respectively) belonging to different instances of class A?

  • My analysis: The answer is yes because T1 and T2 can execute methodA and methodB belonging to different instances of class A and they will not be blocked.

Is my understanding correct as per my analysis for case 1?

Update: Case 2

I have 2 synchronized methods, one is non static and the other is static.

class A {        
    public void synchronized methodA() {}    
    public void static synchronized methodB() {}    
}       

A: I have threads T1 and T2. Can the threads simultaneously execute methodA and methodB (respectively) belonging to the same instance of class A?

  • My analysis: The answer is no because only one method will be executed by T1 and T2 will be blocked until T1 finishes execution.

B: I have threads T1 and T2. Can the threads simultaneously execute methodA and methodB (respectively) belonging to different instances of class A?

  • My analysis: The answer is yes because T1 and T2 can execute methodA and methodB belonging to different instances and they will not be blocked.

Is my understanding correct as per my analysis for case 2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-10-15 03:44:34

情况 A: 否

情况 B: 是

您的分析是正确的。实例方法上的synchronized关键字在功能上等价于this:

public void someMethod() {
   synchronized(this) {
      //code
   }
}

由于this在两个上下文中是不同的,所以它们并不互斥。请注意,即使是相同的方法也可以同时为两个不同的实例调用。

编辑

对于情况2,你在2A上是不正确的;这甚至没有意义。静态同步方法不“属于”类的实例;如果确实如此,它就不是静态的!它不会在类的任何实例上进行同步(它没有对要同步的实例的引用!),因此它在A的类对象上进行同步。类 A 中的静态同步方法相当于:

public static void methodB() {
   synchronized (A.class) {
      //code
   }
}

您的实例方法将在实例上同步,如上面所示,因此两个线程可以并行运行。

在情况 B 中,显然它们也可以并行运行,但您仍然缺少静态方法不与任何实例关联的基本概念。

Case A: No

Case B: Yes

Your analysis is correct. A synchronized keyword on an instance method is functionally equivalent to this:

public void someMethod() {
   synchronized(this) {
      //code
   }
}

Since this is different in the two contexts, they are not mutually exclusive. Note that even the same method could be called at the same time for two different instances.

Edit

For case 2, you are incorrect on 2A; it doesn't even make sense. A static synchronized method does not "belong" to an instance of the class; if it did it wouldn't be static! It doesn't synchronize on any instance of the class (it has no reference to an instance to synchronize on!) so it instead synchronizes on the class object of A. A static synchronized method in class A is equivalent to this:

public static void methodB() {
   synchronized (A.class) {
      //code
   }
}

Your instance method will synchronize on the instance as I've shown above, so the two threads can run in parallel.

In case B then, obviously they also can run in parallel but you're still missing the fundamental concept that a static method is not associated with any instance.

给不了的爱 2024-10-15 03:44:34

情况1:你是对的。

每个 Java 对象(除其他外)都有一个关联的互斥锁(互斥锁)。当 synchronized 应用于非静态方法时,锁定的是实例的互斥锁。因此,同步方法在每个对象的基础上工作。

静态同步方法使用 Class 对象的锁,因此基于每个类(模 ClassLoader)进行操作,并且独立于非静态方法。您还可以在方法体中使用 synchronized() 来使用您想要的任何对象的锁。

情况 2: 你是不正确的,因为 methodA() 和 methodB() 使用不同的互斥体,如上所述。

Case 1: You are correct.

Every Java object has (among other things) an associated mutual exclusion lock (mutex). When synchronized is applied to a non-static method, it is the instance's mutex that is locked. Thus synchronized methods work on a per-object basis.

Static synchronized methods use the lock of the Class object, so operate on a per-class basis (modulo ClassLoader) and independently of non-static methods. You can also use synchronized() in method bodies to use the lock of any object you wish.

Case 2: You are not correct, as methodA() and methodB() use different mutexes, as described above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文