class FineGrainLock { MyMemberClass x, y; Object xlock =new Object(), ylock =new Object(); publicvoid foo() { synchronized(xlock) { //access x here } //do something here - but don't use shared resources synchronized(ylock) { //access y here } } publicvoid bar() { synchronized(this) { //access both x and y here } //do something here - but don't use shared resources } }
4.synchronized后面括号里是类,此时,线程获得的是对象锁.例如:
class ArrayWithLockOrder{ privatestaticlong num_locks =0; privatelong lock_order; privateint[] arr; public ArrayWithLockOrder(int[] a) { arr = a; synchronized(ArrayWithLockOrder.class) {//-----这里 num_locks++; // 锁数加 1。 lock_order = num_locks; // 为此对象实例设置唯一的 lock_order。 } } publiclong lockOrder() { return lock_order; } publicint[] array() { return arr; } } class SomeClass implements Runnable { publicint sumArrays(ArrayWithLockOrder a1, ArrayWithLockOrder a2) { int value =0; ArrayWithLockOrder first = a1; // 保留数组引用的一个 ArrayWithLockOrder last = a2; // 本地副本。 int size = a1.array().length; if (size == a2.array().length) { if (a1.lockOrder() > a2.lockOrder()) // 确定并设置对象的锁定 { // 顺序。 first = a2; last = a1; } synchronized(first) { // 按正确的顺序锁定对象。 synchronized(last) { int[] arr1 = a1.array(); int[] arr2 = a2.array(); for (int i=0; i<size; i++) value += arr1[i] + arr2[i]; } } } return value; } publicvoid run() { // } }