读书人

synchronized : 守则, 推论与实践

发布时间: 2012-11-05 09:35:11 作者: rapoo

synchronized : 规则, 推论与实践

wait without being the recipient of a notification, interruption, or time-out. This is another reason that wait should always be performed in a loop that tests the condition being waited on.



14.7. Deadlocks

One common technique is to use resource ordering. With resource ordering you assign an order on all objects whose locks must be acquired and make sure that you always acquire locks in that order. This makes it impossible for two threads to hold one lock each and be trying to acquire the lock held by the otherthey must both request the locks in the same order, and so once one thread has the first lock, the second thread will block trying to acquire that lock, and then the first thread can safely acquire the second lock.

14.10. The Memory Model: Synchronization and volatile

Rule 1. 除long和double外, 变量的读写都是原子操作; 然而这对于 get / modify / set 操作序列 (像 a++, b--) 毫无帮助, 它们总是需要被同步

The language guarantees that reading or writing any variables, other than those of type long or double, is atomicthe variable will only ever hold a value that was written by some thread, never a partial value intermixing two different writes. This means, for example, that an atomic variable that is only written by one thread and read by many threads need not have access to it synchronized to prevent corruption because there is no possibility of interference. This does not help with getmodifyset sequences (such as ++), which always require synchronization

Rule 2. 原子存取并不意味着一个线程读出来的变量永远是最新的; 事实上, 如果没有同步, 一个线程可能永远都看不见另外一个线程对变量的更新

The rules that determine how memory accesses are ordered and when they are guaranteed to be visible are known as the memory model of the Java programming language. If all reads and writes to a variable occur only when a specific monitor is held, then each read of the variable is guaranteed by the memory model to return the value that was most recently written to it.

Rule 3. 作为第二种同步机制, 使用 volatile 声明的变量能够保证一个线程读出来的变量永远是最新的

There is a second synchronization mechanism that doesn't provide the exclusive access of monitors, but that again ensures that each read of a variable returns the most recently written valuethe use of volatile variables. Fields (but not array elements) can be declared with the volatile modifier. A write to a volatile variable synchronizes with all subsequent reads of that variable. If currentValue was declared as volatile then the example code we showed would be correctly synchronized and the latest value would always be displayed. The use of volatile variables is seldom a replacement for the use of synchronized methods or statements on its own, because they don't provide atomicity across different actions. Rather, volatile variables are most often used for simple flags to indicate something has occurred, or for writing lock-free algorithms that incorporate use of the atomic variables mentioned in Section 25.9.

Rule 4. volatile 另外一个副作用就是让long或double类型的变量读写也变成原子操作


几个最佳实践: A few other synchronization actions help make multithreading work nicely:

Starting a thread synchronizes with the first action performed by that thread when it executes. This ensures that a newly started thread sees any data that was initialized by the creating threadincluding the thread's own fields.

The final action of a thread synchronizes with any action that detects that the thread has terminatedsuch as calling isAlive or invoking join on that thread. This ensures, for example, that if you join a thread you can see all data written by that thread before it terminatedsuch as the results of its computation.

Interrupting a thread synchronizes with any other action that determines that the thread has been interrupted, such as the thread throwing InterruptedException or another thread invoking isInterrupted on the thread.

The write of the default value (zero, null, or false) to any field synchronizes with the first action in any thread. This ensures that even in incorrectly synchronized programs a thread will never see arbitrary values in fieldseither a specific value written by some thread will be seen or the default value of the field will be seen.

读书人网 >其他相关

热点推荐