ReentrantReadWriteLock类的使用

  1. ReentrantReadWriteLock称为读写锁。一个是读相关的锁,也称为共享锁;另外一个是写操作相关的锁,也叫排他锁。使用读写锁的目的是在不需要操作实例变量的方法中提高程序的执行效率
  2. 多个读锁之间不互斥,读锁与写锁互斥,写锁和写锁互斥。

A:读锁和读锁之间共享

package Lock.ReentrantReadWriteLock.ReadRead;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 多个读锁之间不互斥
 */
public class ReadRead {
    private ReentrantReadWriteLock lock=new ReentrantReadWriteLock();//读写锁
    public void read(){
        try {
            lock.readLock().lock();//上锁
            System.out.println("获得读锁: "+Thread.currentThread().getName()+" "+System.currentTimeMillis());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.readLock().unlock();
        }
    }
}
package Lock.ReentrantReadWriteLock.ReadRead;

public class ThreadA extends Thread {
    private ReadRead r;

    //构造函数
    public ThreadA(ReadRead r) {
        this.r = r;
    }

    @Override
    public void run() {
        r.read();
    }
}
package Lock.ReentrantReadWriteLock.ReadRead;

public class ThreadB extends Thread {
    private ReadRead r;

    public ThreadB(ReadRead r) {
        this.r = r;
    }

    @Override
    public void run() {
        r.read();
    }
}

Run方法

package Lock.ReentrantReadWriteLock.ReadRead;

public class Run {
    public static void main(String[] args) {
        ReadRead r=new ReadRead();
        new ThreadA(r).start();
        new ThreadB(r).start();
    }
}

运行结果:

获得读锁: Thread-0 1532583107633
获得读锁: Thread-1 1532583107633

我们可以看到两个线程几乎在同一时刻获得锁,进入锁后面的方法。
使用读锁可以提高程序的执行效率,允许多个线程同时执行,也就是读读共享

B:写锁和写锁之间互斥

package Lock.ReentrantReadWriteLock.WriteWrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 多个读锁之间不互斥
 */
public class WriteWrite


{
    private ReentrantReadWriteLock lock=new ReentrantReadWriteLock();//读写锁
    public void write(){
        try {
            lock.writeLock().lock();//上锁
            System.out.println("获得读锁: "+Thread.currentThread().getName()+" "+System.currentTimeMillis());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.writeLock().unlock();
        }
    }
}
package Lock.ReentrantReadWriteLock.WriteWrite;

public class ThreadA extends Thread {
    private WriteWrite w;

    //构造函数
    public ThreadA(WriteWrite w) {
        this.w = w;
    }

    @Override
    public void run() {
        w.write();
    }
}
package Lock.ReentrantReadWriteLock.WriteWrite;

public class ThreadB extends Thread {
    private WriteWrite w;

    public ThreadB(WriteWrite w) {
        this.w = w;
    }

    @Override
    public void run() {
        w.write();
    }
}
package Lock.ReentrantReadWriteLock.WriteWrite;

public class Run {
    public static void main(String[] args) {
        WriteWrite w=new WriteWrite();
        new ThreadA(w).start();
        new ThreadB(w).start();
    }
}

执行的结果:

获得读锁: Thread-0 1532583423125
获得读锁: Thread-1 1532583425128

写锁的效果是在同一个时刻只有一个线程获得锁执行lock()方法后面的代码

C:读写锁互斥

package Lock.ReentrantReadWriteLock.ReadWrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class readWrite {
    private ReentrantReadWriteLock lock=new ReentrantReadWriteLock();
    public void read(){
        try {
            lock.readLock().lock();
            System.out.println("获得读锁: "+Thread.currentThread().getName()+" "+System.currentTimeMillis());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            System.out.println("读锁释放了");
            lock.readLock().unlock();
        }
    }
    public void write(){
        try{
            lock.writeLock().lock();
            System.out.println("获得写锁: "+Thread.currentThread().getName()+" "+System.currentTimeMillis());
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            System.out.println("写锁释放了");
            lock.writeLock().unlock();
        }
    }
}
package Lock.ReentrantReadWriteLock.ReadWrite;

public class ThreadA extends Thread {
    private readWrite s;

    public ThreadA(readWrite s) {
        this.s = s;
    }

    @Override
    public void run() {
        s.read();
    }
}
package Lock.ReentrantReadWriteLock.ReadWrite;

public class ThreadB extends Thread {
    private readWrite s;

    public ThreadB(readWrite s) {
        this.s = s;
    }

    @Override
    public void run() {
        s.write();
    }
}
package Lock.ReentrantReadWriteLock.ReadWrite;

public class Run {
    public static void main(String[] args) throws InterruptedException {
        readWrite s=new readWrite();
        new ThreadA(s).start();
        Thread.sleep(1000);
        new ThreadB(s).start();
    }
}

运行后的结果:

获得读锁: Thread-0 1532583662479
读锁释放了
获得写锁: Thread-1 1532583664479
写锁释放了

上面的Demo说明读写锁是互斥的,事实上只要出现写锁就是互斥的。

你可能感兴趣的:(ReentrantReadWriteLock类的使用)