如何获取一个对象的监视器的所有权 How does a thread becomes an object's monitor's owner.
From Java Offical Doc:
A thread becomes the owner of the object's monitor in one of three ways:
- By executing a synchronized instance method of that object.
- By executing the body of a
synchronized
statement that synchronizes on the object. - For objects of type
Class,
by executing a synchronized static method of that class.
Only one thread at a time can own an object's monitor.
三种方法:
- 通过执行一个这个对象的synchronized方法
- 通过执行一个同步(synchronize)这个对象的 同步(synchronized)代码块
- 对于Class类型的对象,通过执行一个这个class的静态同步方法。
一次只能有一个线程可以拥有一个对象的监视器。
Java 内存一致特性 的 "happens-before" ( 摘自Java文档):
Chapter 17 of The Java™ Language Specification defines the happens-before relation on memory operations such as reads and writes of shared variables. The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation. The synchronized
and volatile
constructs, as well as the Thread.start()
and Thread.join()
methods, can form happens-before relationships. In particular:
- Each action in a thread happens-before every action in that thread that comes later in the program's order.
- An unlock (
synchronized
block or method exit) of a monitor happens-before every subsequent lock (synchronized
block or method entry) of that same monitor. And because the happens-before relation is transitive, all actions of a thread prior to unlocking happen-before all actions subsequent to any thread locking that monitor. - A write to a
volatile
field happens-before every subsequent read of that same field. Writes and reads ofvolatile
fields have similar memory consistency effects as entering and exiting monitors, but do not entail mutual exclusion locking. - A call to
start
on a thread happens-before any action in the started thread. - All actions in a thread happen-before any other thread successfully returns from a
join
on that thread.
The methods of all classes in java.util.concurrent
and its subpackages extend these guarantees to higher-level synchronization. In particular:
- Actions in a thread prior to placing an object into any concurrent collection happen-before actions subsequent to the access or removal of that element from the collection in another thread.
- Actions in a thread prior to the submission of a
Runnable
to anExecutor
happen-before its execution begins. Similarly forCallables
submitted to anExecutorService
. - Actions taken by the asynchronous computation represented by a
Future
happen-before actions subsequent to the retrieval of the result viaFuture.get()
in another thread. - Actions prior to "releasing" synchronizer methods such as
Lock.unlock
,Semaphore.release
, andCountDownLatch.countDown
happen-before actions subsequent to a successful "acquiring" method such asLock.lock
,Semaphore.acquire
,Condition.await
, andCountDownLatch.await
on the same synchronizer object in another thread. - For each pair of threads that successfully exchange objects via an
Exchanger
, actions prior to theexchange()
in each thread happen-before those subsequent to the correspondingexchange()
in another thread. - Actions prior to calling
CyclicBarrier.await
andPhaser.awaitAdvance
(as well as its variants) happen-before actions performed by the barrier action, and actions performed by the barrier action happen-before actions subsequent to a successful return from the correspondingawait
in other threads.
Since:
1.5
一个线程的写保证对另外一个线程可见只要写操作发生在读操作之前。 同步(synchronized)与voltaile数据结构以及Thread.start(), Thread.join()方法可以构成'happens-before'关系。特别地:
Java Static块中的内容会在类被ClassLaoder加载的时候仅被执行一次。特别地:如果static块中有启动子线程的话,子线程会在主线程执行完static块之后再开始执行。