java 对象 lock_Java:使用synchronized和Lock对象获取对象锁

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();

}

}

来源:考试大-Java认证

责编:xxm  评论 纠错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值