程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java中使用synchronized和Lock對象獲取對象鎖

Java中使用synchronized和Lock對象獲取對象鎖

編輯:關於JAVA

在並發環境下,解決共享資源沖突問題時,可以考慮使用鎖機制。

1.對象的鎖

所有對象都自動含有單一的鎖。

JVM負責跟蹤對象被加鎖的次數。如果一個對象被解鎖,其計數變為0。在任務(線程)第一次給對象加鎖的時候,計數變為1。每當這個相同的任務(線程)在此對象上獲得鎖時,計數會遞增。

只有首先獲得鎖的任務(線程)才能繼續獲取該對象上的多個鎖。

每當任務離開一個synchronized方法,計數遞減,當計數為0的時候,鎖被完全釋放,此時別的任務就可以使用此資源。

2.synchronized同步塊

2.1同步到單一對象鎖

當使用同步塊時,如果方法下的同步塊都同步到一個對象上的鎖,則所有的任務(線程)只能互斥的進入這些同步塊。

Resource1.java演示了三個線程(包括main線程)試圖進入某個類的三個不同的方法的同步塊中,雖然這些同步塊處在不同的方法中,但由於是同步到同一個對象(當前對象 synchronized (this)),所以對它們的方法依然是互斥的。

Resource1.java

package com.zj.lock;
import java.util.concurrent.TimeUnit;

public class Resource1 {
public void f() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in f()");
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in f()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public void g() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in g()");
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in g()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public void h() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in h()");
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in h()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
final Resource1 rs = new Resource1();

new Thread() {
public void run() {
rs.f();
}
}.start();

new Thread() {
public void run() {
rs.g();
}
}.start();

rs.h();
}
}

結果:

Thread-0:not synchronized in f()

Thread-0:synchronized in f()

main:not synchronized in h()

Thread-1:not synchronized in g()

Thread-0:synchronized in f()

Thread-0:synchronized in f()

Thread-0:synchronized in f()

Thread-0:synchronized in f()

Thread-1:synchronized in g()

Thread-1:synchronized in g()

Thread-1:synchronized in g()

Thread-1:synchronized in g()

Thread-1:synchronized in g()

main:synchronized in h()

main:synchronized in h()

main:synchronized in h()

main:synchronized in h()

main:synchronized in h()

2.2 同步到多個對象鎖

Resource1.java演示了三個線程(包括main線程)試圖進入某個類的三個不同的方法的同步塊中,這些同步塊處在不同的方法中,並且是同步到三個不同的對象(synchronized (this),synchronized (syncObject1),synchronized (syncObject2)),所以對它們的方法中的臨界資源訪問是獨立的。

Resource2.java

package com.zj.lock;
import java.util.concurrent.TimeUnit;

public class Resource2 {
private Object syncObject1 = new Object();
private Object syncObject2 = new Object();

public void f() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in f()");
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in f()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public void g() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in g()");
synchronized (syncObject1) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in g()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public void h() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in h()");
synchronized (syncObject2) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in h()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
final Resource2 rs = new Resource2();

new Thread() {
public void run() {
rs.f();
}
}.start();

new Thread() {
public void run() {
rs.g();
}
}.start();

rs.h();
}
}

結果:

Thread-0:not synchronized in f()

Thread-0:synchronized in f()

main:not synchronized in h()

main:synchronized in h()

Thread-1:not synchronized in g()

Thread-1:synchronized in g()

Thread-0:synchronized in f()

main:synchronized in h()

Thread-1:synchronized in g()

Thread-0:synchronized in f()

main:synchronized in h()

Thread-1:synchronized in g()

Thread-0:synchronized in f()

main:synchronized in h()

Thread-1:synchronized in g()

Thread-0:synchronized in f()

main:synchronized in h()

Thread-1:synchronized in g()

3.Lock對象鎖

除了使用synchronized外,還可以使用Lock對象來創建臨界區。Resource3.java的演示效果同Resource1.java;Resource4.java的演示效果同Resource2.java。

Resource3.java

package com.zj.lock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Resource3 {
private Lock lock = new ReentrantLock();

public void f() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in f()");
lock.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in f()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock.unlock();
}
}

public void g() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in g()");
lock.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in g()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock.unlock();
}
}

public void h() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in h()");
lock.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in h()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock.unlock();
}
}

public static void main(String[] args) {
final Resource3 rs = new Resource3();

new Thread() {
public void run() {
rs.f();
}
}.start();

new Thread() {
public void run() {
rs.g();
}
}.start();

rs.h();
}
}

結果:

Thread-0:not synchronized in f()

Thread-0:synchronized in f()

main:not synchronized in h()

Thread-1:not synchronized in g()

Thread-0:synchronized in f()

Thread-0:synchronized in f()

Thread-0:synchronized in f()

Thread-0:synchronized in f()

main:synchronized in h()

main:synchronized in h()

main:synchronized in h()

main:synchronized in h()

main:synchronized in h()

Thread-1:synchronized in g()

Thread-1:synchronized in g()

Thread-1:synchronized in g()

Thread-1:synchronized in g()

Thread-1:synchronized in g()

Resource4.java

package com.zj.lock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Resource4 {
private Lock lock1 = new ReentrantLock();
private Lock lock2 = new ReentrantLock();
private Lock lock3 = new ReentrantLock();

public void f() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in f()");
lock1.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in f()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock1.unlock();
}
}

public void g() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in g()");
lock2.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in g()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock2.unlock();
}
}

public void h() {
// other operations should not be locked...
System.out.println(Thread.currentThread().getName()
+ ":not synchronized in h()");
lock3.lock();
try {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()
+ ":synchronized in h()");
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock3.unlock();
}
}

public static void main(String[] args) {
final Resource4 rs = new Resource4();

new Thread() {
public void run() {
rs.f();
}
}.start();

new Thread() {
public void run() {
rs.g();
}
}.start();

rs.h();
}
}

結果:

Thread-0:not synchronized in f()

Thread-0:synchronized in f()

main:not synchronized in h()

main:synchronized in h()

Thread-1:not synchronized in g()

Thread-1:synchronized in g()

Thread-0:synchronized in f()

main:synchronized in h()

Thread-1:synchronized in g()

Thread-0:synchronized in f()

main:synchronized in h()

Thread-1:synchronized in g()

Thread-0:synchronized in f()

main:synchronized in h()

Thread-1:synchronized in g()

Thread-0:synchronized in f()

main:synchronized in h()

Thread-1:synchronized in g()

本文出自 “子 孑” 博客,請務必保留此出處http://zhangjunhd.blog.51cto.com/113473/70300

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved