JVM負責跟蹤對象被加鎖的次數
只有首先獲得鎖的任務(線程)才能繼續獲取該對象上的多個鎖
每當任務離開一個synchronized方法
Resource
Resource
package com
import ncurrent
public class Resource
public void f() {
// other operations should not be locked
System
+
synchronized (this) {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
}
}
public void g() {
// other operations should not be locked
System
+
synchronized (this) {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
}
}
public void h() {
// other operations should not be locked
System
+
synchronized (this) {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
}
}
public static void main(String[] args) {
final Resource
new Thread() {
public void run() {
rs
}
}
new Thread() {
public void run() {
rs
}
}
rs
}
}
結果
Thread
Thread
main:not synchronized in h()
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
Thread
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
Resource
Resource
package com
import ncurrent
public class Resource
private Object syncObject
private Object syncObject
public void f() {
// other operations should not be locked
System
+
synchronized (this) {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
}
}
public void g() {
// other operations should not be locked
System
+
synchronized (syncObject
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
}
}
public void h() {
// other operations should not be locked
System
+
synchronized (syncObject
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
}
}
public static void main(String[] args) {
final Resource
new Thread() {
public void run() {
rs
}
}
new Thread() {
public void run() {
rs
}
}
rs
}
}
結果
Thread
main:not synchronized in h()
main:synchronized in h()
Thread
Thread
Thread
main:synchronized in h()
Thread
Thread
main:synchronized in h()
Thread
Thread
main:synchronized in h()
Thread
Thread
main:synchronized in h()
Thread
除了使用synchronized外
Resource
Resource
package com
import ncurrent
import ncurrent
import ncurrent
public class Resource
private Lock lock = new ReentrantLock();
public void f() {
// other operations should not be locked
System
+
lock
try {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
} finally {
lock
}
}
public void g() {
// other operations should not be locked
System
+
lock
try {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
} finally {
lock
}
}
public void h() {
// other operations should not be locked
System
+
lock
try {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
} finally {
lock
}
}
public static void main(String[] args) {
final Resource
new Thread() {
public void run() {
rs
}
}
new Thread() {
public void run() {
rs
}
}
rs
}
}
結果
Thread
Thread
main:not synchronized in h()
Thread
Thread
Thread
Thread
Thread
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
Thread
Thread
Thread
Thread
Thread
Resource
package com
import ncurrent
import ncurrent
import ncurrent
public class Resource
private Lock lock
private Lock lock
private Lock lock
public void f() {
// other operations should not be locked
System
+
lock
try {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
} finally {
lock
}
}
public void g() {
// other operations should not be locked
System
+
lock
try {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
} finally {
lock
}
}
public void h() {
// other operations should not be locked
System
+
lock
try {
for (int i =
System
+
try {
TimeUnit
} catch (InterruptedException e) {
e
}
}
} finally {
lock
}
}
public static void main(String[] args) {
final Resource
new Thread() {
public void run() {
rs
}
}
new Thread() {
public void run() {
rs
}
}
rs
}
}
結果
Thread
Thread
main:not synchronized in h()
main:synchronized in h()
Thread
Thread
Thread
main:synchronized in h()
Thread
Thread
main:synchronized in h()
Thread
Thread
main:synchronized in h()
Thread
Thread
main:synchronized in h()
Thread
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26317.html