简体   繁体   English

同步是否适用于 class 中的所有方法或仅适用于 1?

[英]Does synchronized work for all methods in a class or only 1?

eg thread 1 from class 1 accesses synchronized method 1 of sub class A. Then thread 2 from class 2 accessed synchronized method 1 from same sub class A. Everything is fine here. eg thread 1 from class 1 accesses synchronized method 1 of sub class A. Then thread 2 from class 2 accessed synchronized method 1 from same sub class A. Everything is fine here.

If I have thread 3 from class 3 access the synchronized method 2 of sub class A, will it still forbid thread 1 and thread 2 from accessing method 1 while method 2 is doing its thing?如果我有来自 class 3 的线程 3 访问子 class A 的同步方法 2,它是否仍会禁止线程 1 和线程 2 在方法 2 执行其操作时访问方法 1? If not, how can I achieve that?如果没有,我该如何实现?

The JLS, § 17.1 is quite verbose about this: JLS,第 17.1 节对此非常冗长:

A synchronized method ( §8.4.3.6 ) automatically performs a lock action when it is invoked;同步方法( 第 8.4.3.6 节)在调用时会自动执行锁定操作; its body is not executed until the lock action has successfully completed.在锁定操作成功完成之前,它的主体不会被执行。 If the method is an instance method, it locks the monitor associated with the instance for which it was invoked (that is, the object that will be known as this during execution of the body of the method).如果该方法是一个实例方法,它会锁定与调用它的实例相关联的监视器(即,在方法主体的执行期间将被称为this的 object)。 If the method is static, it locks the monitor associated with the Class object that represents the class in which the method is defined.如果方法是 static,它会锁定与 Class object 关联的监视器,该方法代表 ZA2F2ED4F8EBC0CBB4C21A29DC 中定义的方法。 If execution of the method's body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.如果方法主体的执行已经完成,无论是正常的还是突然的,都会在同一个监视器上自动执行解锁操作。

Oracle's official tutorial on synchronized Methods phrases it a little bit more understandable: Oracle 的关于同步方法的官方教程用短语来解释它更容易理解:

... ...

  • First, it is not possible for two invocations of synchronized methods on the same object to interleave.首先,同一 object 上的同步方法的两次调用不可能交错。 ... ...

... ...

A synchronized method is just a shortcut way to write a method whose body is a synchronized block. synchronized方法只是编写主体为synchronized块的方法的一种快捷方式。 This;这个;

synchronized AnyType foobar(...) {
    doSomething();
}

Is just a shortcut way of writing this;只是写这个的捷径;

AnyType foobar(...) {
    synchronized(this) {
        doSomething();
    }
}

More detail can be found in Java synchronized method更详细的可以看Java同步方法


PS, PS,

If I have thread 3 from class 3...如果我有来自 class 3 的线程 3 ...

Threads aren't "from classes."线程不是“来自类”。 Threads are just objects that execute your code.线程只是执行代码的对象。 When your program creates a new thread, the system doesn't remember or care what method of what class created it, and the system doesn't even remember or care what other thread created it.当你的程序创建一个新线程时,系统不记得也不关心 class 是用什么方法创建的,系统甚至不记得也不关心其他线程是什么创建的。

You ask (assuming instance methods and not static methods):你问(假设实例方法而不是 static 方法):

Thread 1 ---executes--> classA.method1
Thread 2 ---executes--> classA.method1

then然后

Thread 3 ---executes--> classA.method2

Both method1 and method2 are synchronized instance methods in the same class. method1 和 method2 都是同一个 class 中的同步实例方法。

Result : Both method1 and method2 have the same monitor (the this instance) and one thread at a time will get the lock and execute method1 or method2 and no other thread will be able to unless the thread executing the methods exits and the monitor becomes available to be locked.结果method1method2都有相同的监视器this实例),一次一个线程将获得锁并执行method1method2 ,除非执行方法的线程退出并且监视器可用,否则其他线程将无法执行被锁定。

The key to understanding is that the monitor is the same for all the two methods.理解的关键是监视器对于所有两种方法都是相同的。

暂无
暂无

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

相关问题 同步方法 - 这是如何工作的? - Synchronized methods - how does this work? 当他们说StringBuffer类已同步时,这到底是什么意思? 是否已声明该类的所有方法同步? - What exactly does it mean when they say that StringBuffer class is synchronized? Are all methods of that class declared synchronized? Java Synchronized会同步一个类的所有同步方法吗? - Java Synchronized synchronizes all synchronized methods of a class among each other? 如何使所有类方法仅在单个线程上运行? (同步类?) - How to make all class methods run only on single thread? (synchronized class?) synchronized(this)是否仅锁定同步块或所有“this”代码? - Does synchronized (this) lock only the synchronized block or all the “this” code? 具有属性和同步方法的静态类 - Static Class with attributes and synchronized methods 为什么Observable类中的方法是同步的? - Why methods in Observable class are synchronized? 在Java中,当对象中的一个方法“同步”时,所有方法都是“同步”的吗? - In Java when one methods in an object is 'synchronized' are all methods 'synchronized'? 为什么 findViewById() 方法只在活动 class 的方法中有效,而在 class 中直接无效? - Why does the findViewById() method work only in the methods of the activity class but not in the class directly? 仅具有私有字段的 object + 所有同步的方法都可以命名为线程安全吗? - Can object having private fields only + all methods synchronized be named as thread safe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM