简体   繁体   English

一次有多少个线程可以访问此Java对象的同步代码?

[英]How many threads may have access to the synchronized code of this Java object at any one time?

Assume the following class: 假设下面的类:

public class MyClass {
  public synchronized void methodA() {
    //...code
  }
  public synchronized void methodB() {
    //...code
  }
  public synchronized void methodC() {
    //...code
  } 
}

Assume that none of the synchronized methods of MyClass call each other. 假定MyClass的所有同步方法都不相互调用。

Do I assume correctly that 1, and only 1 Thread may have access to ANY of the code of MyClass at any given time? 我是否正确地假设1,并且在任何给定时间只有1个线程可以访问MyClass的任何代码? If, for example, a thread is executing methodA() on an instance of MyClass, I assume that no other Thread may call either methodB() or methodC() at this time, but will block until the execution of methodA() by the first thread is complete. 例如,如果某个线程正在MyClass的实例上执行methodA(),则我认为此时没有其他线程可以调用methodB()或methodC(),但是将阻塞直到该线程执行methodA()为止。第一线程完成。

I want to clarify my understanding of Goetz, Java Concurrency in Practice (28), who states: 我想澄清一下我对Java并发实践Goetz(28)的理解,他指出:

"Acquiring the lock associated with an object does not prevent other threads from accessing that object--the only thing that acquiring a lock prevents any other thread from doing is acquiring that same lock." “获取与对象相关联的锁不会阻止其他线程访问该对象,获取锁会阻止其他任何线程执行的唯一操作就是获取相同的锁。”

In this particular case, I argue, Goetz' first statement is incorrect. 我认为在这种特殊情况下,Goetz的第一句话是不正确的。 MyClass employs implicit locking on each of its 3 methods--for each method the implicit lock is itself ( this ). MyClass在其3个方法中的每个方法上都使用隐式锁定-对于每个方法,隐式锁定本身就是( this )。 Therefore, in this case, if a thread holds the implicit lock while executing methodA(), all other threads will be prevented from accessing any of the code on this object. 因此,在这种情况下,如果线程在执行methodA()时持有隐式锁,则将阻止所有其他线程访问此对象上的任何代码。

Do I understand method synchronization and implicit locking correctly? 我是否正确理解方法同步和隐式锁定?

Any perspectives are greatly appreciated! 任何观点都将不胜感激!

"Acquiring the lock associated with an object does not prevent other threads from accessing that object--the only thing that acquiring a lock prevents any other thread from doing is acquiring that same lock." “获取与对象相关联的锁不会阻止其他线程访问该对象,获取锁会阻止其他任何线程执行的唯一操作就是获取相同的锁。”

Your three methods are declared as being synchronized . 您的三个方法都声明为已synchronized When one of those methods is invoked, like 当这些方法之一被调用时,例如

MyClass instance = new MyClass();
instance.methodA();

the currently executing thread (call it A) will acquire that instance's monitor. 当前正在执行的线程(称为A)将获取该实例的监视器。 No other thread will be able to acquire the same object's monitor until A has released it. 除非A释放了其他对象,否则其他线程将无法获取该对象的监视器。 Nothing prevents other threads from calling methods on that same object. 没有什么可以阻止其他线程在同一对象上调用方法。

Say you had thread A doing 假设您有线程A在做

instance.methodA();

and thread B doing 和线程B做

instance.methodB();

Then, yes, thread B would have to wait for methodA to complete (ie. for thread A to release the lock on the object referenced by instance ). 然后,是的,线程B将不得不等待methodA完成(即,线程A释放instance引用的对象上的锁)。

However, if thread A was doing 但是,如果线程A在做

instance.methodA();

and thread B was doing 而线程B在做

instance.toString();

thread B would not have to wait at all since toString() does nothing with the object's monitor. 线程B完全不必等待,因为toString()对对象的监视器不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM