什么事件会使等待线程执行另一个线程已经 运行 的同步方法?
What event will make a waiting thread execute a synchronized method already run by another thread?
在 Java 编程中,如果线程 A 在尝试执行另一个线程 B 正在执行的同一对象的 synchronized
方法时被阻塞,什么事件将使它成为可能线程A执行同步方法?
线程B已经执行完方法这个简单的事实足以让线程A从等待状态介入并执行方法吗?或者我需要在同步方法中的某处调用 notifyAll
或 notify
吗?
Java教程说:
When one thread is executing a synchronized
method for an object, all
other threads that invoke synchronized
methods for the same object
block (suspend execution) until the first thread is done with the
object.
Java 语言参考说(强调我的):
8.4.3.6. synchronized
Methods
A synchronized
method acquires a monitor (§17.1) before it executes.
For a class (static) method, the monitor associated with the Class
object for the method's class is used.
For an instance method, the monitor associated with this (the object
for which the method was invoked) is used.
17.1。同步
The Java programming language provides multiple mechanisms for
communicating between threads. The most basic of these methods is
synchronization, which is implemented using monitors. Each object in
Java is associated with a monitor, which a thread can lock or unlock.
Only one thread at a time may hold a lock on a monitor. Any other
threads attempting to lock that monitor are blocked until they can
obtain a lock on that monitor. A thread t may lock a particular
monitor multiple times; each unlock reverses the effect of one lock
operation.
The synchronized
statement (§14.19) computes a reference to an object;
it then attempts to perform a lock action on that object's monitor and
does not proceed further until the lock action has successfully
completed. After the lock action has been performed, the body of the
synchronized
statement is executed. If execution of the body is ever
completed, either normally or abruptly, an unlock action is
automatically performed on that same monitor.
14.19。同步语句
A synchronized
statement acquires a mutual-exclusion lock (§17.1) on
behalf of the executing thread, executes a block, then releases the
lock. While the executing thread owns the lock, no other thread may
acquire the lock.
在 Java 编程中,如果线程 A 在尝试执行另一个线程 B 正在执行的同一对象的 synchronized
方法时被阻塞,什么事件将使它成为可能线程A执行同步方法?
线程B已经执行完方法这个简单的事实足以让线程A从等待状态介入并执行方法吗?或者我需要在同步方法中的某处调用 notifyAll
或 notify
吗?
Java教程说:
When one thread is executing a
synchronized
method for an object, all other threads that invokesynchronized
methods for the same object block (suspend execution) until the first thread is done with the object.
Java 语言参考说(强调我的):
8.4.3.6.
synchronized
MethodsA
synchronized
method acquires a monitor (§17.1) before it executes.For a class (static) method, the monitor associated with the Class object for the method's class is used.
For an instance method, the monitor associated with this (the object for which the method was invoked) is used.
17.1。同步
The Java programming language provides multiple mechanisms for communicating between threads. The most basic of these methods is synchronization, which is implemented using monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor. Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor. A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation.
The
synchronized
statement (§14.19) computes a reference to an object; it then attempts to perform a lock action on that object's monitor and does not proceed further until the lock action has successfully completed. After the lock action has been performed, the body of thesynchronized
statement is executed. If execution of the body is ever completed, either normally or abruptly, an unlock action is automatically performed on that same monitor.14.19。同步语句
A
synchronized
statement acquires a mutual-exclusion lock (§17.1) on behalf of the executing thread, executes a block, then releases the lock. While the executing thread owns the lock, no other thread may acquire the lock.