[英]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.结果: method1
和method2
都有相同的监视器( this
实例),一次一个线程将获得锁并执行method1
或method2
,除非执行方法的线程退出并且监视器可用,否则其他线程将无法执行被锁定。
The key to understanding is that the monitor is the same for all the two methods.理解的关键是监视器对于所有两种方法都是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.