ReentrantLock 类

3次阅读

共计 4033 个字符,预计需要花费 11 分钟才能阅读完成。

在 Java 多线程中, 可以使用 synchronized 关键字来实现多线程之间同步互斥, 但在 JDK 1.5 中新增加了 ReentrantLock 类也能达到同样的效果, 并且在扩展功能上也更加强大, 比如具有嗅探锁定, 多路分支通知等功能, 而且在使用上也比 synchronized 更加的灵活.
使用 ReentrantLock 实现同步
public class MyService {

private Lock lock = new ReentrantLock();

public void testMethod() {
lock.lock();

for (int i = 0; i < 10; i++){
System.out.println(“ThreadName=” + Thread.currentThread().getName() + (” ” + (i + 1)));
}

lock.unlock();

}

}
public class MyThread extends Thread {

private MyService myService;
public MyThread(MyService myService) {
this.myService = myService;
}

@Override
public void run() {
myService.testMethod();
}
}
public static void main(String[] args) throws IOException, InterruptedException {

MyService myService = new MyService();

MyThread myThreadA = new MyThread(myService);
MyThread myThreadB = new MyThread(myService);
MyThread myThreadC = new MyThread(myService);
MyThread myThreadD = new MyThread(myService);
MyThread myThreadE = new MyThread(myService);

myThreadA.start();
myThreadB.start();
myThreadC.start();
myThreadD.start();
myThreadE.start();

}
调用 ReentrantLock 对象的 lock() 方法获取锁, 调用 unLock() 方法释放锁.
从运行结果来看, 当前线程打印完毕之后将锁进行释放, 其他的线程才可以继续打印. 线程打印的数据是分组打印, 因为当前线程已经持有锁, 但线程之间打印的顺序是随机的.
使用 Condition 实现等待 / 通知
关键字 synchronized 与 wait() 和 notify() / notifyall() 方法结合可以实现等待 / 通知模式, 只不过在使用时, 调用 notify() 方法 JVM 会随机选择一个 WAITNG 状态的线程来执行.
而使用 Condition 则可以更加灵活, 可以实现 “ 选择性通知 ”, 可以指定的选择唤醒哪些线程, 哪些线程继续等待.
public class MyService {

private Lock lock = new ReentrantLock();
public Condition conditionA = lock.newCondition();
public Condition conditionB = lock.newCondition();

public void awaitA() throws InterruptedException {
lock.lock();

System.out.println(“begin awaitA 时间 ” + System.currentTimeMillis() + “ThreadName=” + Thread.currentThread().getName());

conditionA.await();

System.out.println(“end awaitA 时间 ” + System.currentTimeMillis() + “ThreadName=” + Thread.currentThread().getName());

lock.unlock();
}

public void awaitB() throws InterruptedException {
lock.lock();

System.out.println(“begin awaitB 时间 ” + System.currentTimeMillis() + “ThreadName=” + Thread.currentThread().getName());

conditionB.await();

System.out.println(“end awaitB 时间 ” + System.currentTimeMillis() + “ThreadName=” + Thread.currentThread().getName());

lock.unlock();
}

public void signalAll_A() throws InterruptedException {
lock.lock();
System.out.println(“begin signalAll_A 时间 ” + System.currentTimeMillis() + “ThreadName=” + Thread.currentThread().getName());

conditionA.signalAll();

lock.unlock();
}

public void signalAll_B() throws InterruptedException {
lock.lock();
System.out.println(“begin signalAll_B 时间 ” + System.currentTimeMillis() + “ThreadName=” + Thread.currentThread().getName());

conditionB.signalAll();

lock.unlock();
}
}
public class ThreadA extends Thread {

private MyService myService;
public ThreadA(MyService myService) {
this.myService = myService;
}

@Override
public void run() {
try {
myService.awaitA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ThreadB extends Thread {

private MyService myService;

public ThreadB(MyService myService) {
this.myService = myService;
}

@Override
public void run() {
try {
myService.awaitB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException, InterruptedException {

MyService myService = new MyService();

ThreadA threadA = new ThreadA(myService);
threadA.setName(“a”);
threadA.start();

ThreadB threadB = new ThreadB(myService);
threadB.setName(“b”);
threadB.start();

Thread.sleep(3000);
myService.signalAll_A();

}

Object 类中的 wait() 方法相当于 Condition 类中的 await() 方法.
Object 类中的 wait(long timeout) 方法相当于 Condition 类中的 await(long time, TimeUnit unit) 方法.
Object 类中的 notify() 方法相当于 Condition 类中的 signal() 方法.
Object 类中的 notifyAll() 方法相当于 Condition 类中的 signalAll() 方法.

从执行结果来看, a 和 b 线程被暂停, 当执行 myService.signalAll_A() 方法时, a 线程继续执行, 而 b 线程仍然是等待状态.
常用方法
ReentrantLock 类
int getHoldCount() 查询调用 lock() 方法的次数.
final int getQueueLength() 估计等待锁的线程数. 比如有 5 个线程, 1 个线程首先执行 await() 方法, 那么在调用此方法后返回值是 4, 说明有 4 个线程同时在等待 lock 的释放.
int getWaitQueueLength(Condition condition) 返回与此锁相关联给定条件等待的线程数的估计. 比如有 5 个线程, 每个线程都执行了同一个 condition 对象的 await() 方法, 则调用此方法时返回的值是 5.
final boolean hasQueuedThreads() 判断是否有线程等待此锁.
final boolean hasQueuedThread(Thread thread) 判断指定线程是否等待获取此锁.
boolean hasWaiters(Condition condition) 判断线程有没有调用 await() 方法.
void lockInterruptibly() throws InterruptedException 获取锁, 除非当前线程为 interrupted.
Condition 类
void awaitUninterruptibly() 和 await() 区别就是当调用 interrupt() 方法时不会抛出 InterrputedException 异常.

正文完
 0