Java 多线程(六):ReentrantLock 与 Condition

ReentrantLock

  • java.util.concurrent.lock 中的 Lock 是锁的顶层接口,它允许把锁定的实现作为 Java 类,而不是作为语言特性来实现,这带来了更多的灵活性,可以只对某个代码块进行加锁,而不是整个方法,ReentrantLock 是 Lock 的一种实现
  • 它有几个主要的方法:
public interface Lock {
    void lock();//获得锁,如果锁已经被占用则等待,必须等待当前线程结束才会响应其它线程的中断
    void lockInterruptibly() throws InterruptedException;//获得锁,但如果检测到 interrupt 标志为 true 则立刻抛出 InterruptedException 异常
    boolean tryLock();// 尝试获得锁,如果成功,返回true,失败返回false。该方法不等待,立即返回
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException; //   在给定时间内尝试获得锁
    void unlock();   //用来释放锁
    Condition newCondition();
}
  • 注意:解锁操作要放到 finally 中,这样保证即使抛出了异常锁也必须释放,否则其它线程将永远阻塞

用法

  • 下面例子中,线程2必须等待线程1释放锁后才能执行锁内的代码
public class LockTest implements Runnable{

    public static ReentrantLock reentrantLock = new ReentrantLock();

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getId()+"进入方法");
        try {
            // reentrantLock.lockInterruptibly();  检测到 interrupt 标志则立刻抛出异常
            reentrantLock.lock();
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getId()+"执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        LockTest lockTest = new LockTest();
        new Thread(lockTest).start();
        new Thread(lockTest).start();
    }

}

公平锁和非公平锁

  • 公平锁是指多个线程等待同一个锁时,必须按照申请锁的先后顺序来获得锁
  • 非公平锁是指可以不按照顺序,抢占锁
// 构造参数为 true 为公平锁(默认),false 为非公平锁
public static ReentrantLock reentrantLock = new ReentrantLock( false);

Condition

  • Condition 的功能与 wait() 和 notify() 方法差不多,但前者是配合 ReentrantLock 使用的,后者是配合 synchronized 使用的
  • 它几个主要的方法:
void await() throws InterruptedException; // 使当前线程等待,同时释放锁,当其它线程中使用 singal() 和 signalAll() 方法时,线程会重新获得锁并继续执行,或者当前线程中断时也跳出等待
void awaitUninterruptibly();    // 与 await() 方法基本相同,但不会响应中断
void signal();  // 唤醒一个在等待的线程
void signalAll();   // 唤醒所有在等待的线程

用法

  • 下面代码中线程1会进入等待状态,并释放锁,直到线程2调用 signal() 方法重新唤醒时才继续执行
public class LockTest implements Runnable{

    public static ReentrantLock reentrantLock = new ReentrantLock( );
    public static Condition condition = reentrantLock.newCondition();

    public Boolean flag;

    public LockTest(Boolean flag) {
        this.flag = flag;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getId()+"进入方法");
        try {
            reentrantLock.lock();
            System.out.println(Thread.currentThread().getId()+"执行中");
            if(flag)
                condition.signal();
            else
                condition.await();
            System.out.println(Thread.currentThread().getId()+"执行完成");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        LockTest l1 = new LockTest(false)
        new Thread(l1).start();
        LockTest l2 = new LockTest(true);
        new Thread(l2).start();
    }

}

你可能感兴趣的:(Java 多线程(六):ReentrantLock 与 Condition)