线程类Thread的API接口分析系列之读写锁ReentrantReadeWriteLock

    xiaoxiao2021-03-25  133

    读写锁的使用相对简单,原则也好记,那就是读读之间不互斥,读写之间互斥,直接上例子吧

    读读之间:

    public class ReadeWriteLockTest implementsRunnable {

            

             ReentrantReadWriteLocklock = new ReentrantReadWriteLock();

     

             /*@author : zhengrf1

              * @date 创建时间:2017年3月9日 上午10:19:06

              * @see java.lang.Runnable#run()

              */

             @Override

             publicvoid run() {

                       //TODO Auto-generated method stub

                       lock.readLock().lock();

                       System.out.println(Thread.currentThread().getName()+"获取到 读锁,休息10秒");

                       try{

                                Thread.sleep(10000);

                       }catch (InterruptedException e) {

                                //TODO Auto-generated catch block

                                e.printStackTrace();

                       }

                       lock.readLock().unlock();

                       System.out.println(Thread.currentThread().getName()+"释放锁");

             }

     

             /**

             *

             *@author : zhengrf1

             *@date 创建时间:2017年3月9日上午10:19:06

             */

             publicstatic void main(String[] args) {

                       //TODO Auto-generated method stub

                       ReadeWriteLockTesttest = new ReadeWriteLockTest();

                       Threadtest1  = new Thread(test);

                       Threadtest2  = new Thread(test);

                       test1.start();

                       test2.start();

             }

    }

    --输出:

    Thread-1获取到读锁,休息10秒

    Thread-0获取到读锁,休息10秒

    Thread-0释放锁

    Thread-1释放锁

    ---说明读读锁之间是不互斥的

    写写之间

    public class ReadeWriteLockTest implementsRunnable {

            

             ReentrantReadWriteLocklock = new ReentrantReadWriteLock();

     

             /*@author : zhengrf1

              * @date 创建时间:2017年3月9日 上午10:19:06

              * @see java.lang.Runnable#run()

              */

             @Override

             publicvoid run() {

                       //TODO Auto-generated method stub

                       lock.writeLock().lock();

                       System.out.println(Thread.currentThread().getName()+"获取到 读锁,休息10秒");

                       try{

                                Thread.sleep(10000);

                       }catch (InterruptedException e) {

                                //TODO Auto-generated catch block

                                e.printStackTrace();

                       }

                       lock.writeLock().unlock();

                       System.out.println(Thread.currentThread().getName()+"释放锁");

             }

     

             /**

             *

             *@author : zhengrf1

             *@date 创建时间:2017年3月9日上午10:19:06

             */

             publicstatic void main(String[] args) {

                       //TODO Auto-generated method stub

                       ReadeWriteLockTesttest = new ReadeWriteLockTest();

                       Threadtest1  = new Thread(test);

                       Threadtest2  = new Thread(test);

                       test1.start();

                       test2.start();

             }

    }

    输出:

    Thread-0获取到读锁,休息10秒

    Thread-0释放锁

    Thread-1获取到读锁,休息10秒

    Thread-1释放锁

    --说明写写之间互斥

    写读之间:

    public class ReadeWriteLockTest implementsRunnable {

            

             staticReentrantReadWriteLock lock = new ReentrantReadWriteLock();

             Stringtype;

     

             /*@author : zhengrf1

              * @date 创建时间:2017年3月9日 上午10:19:06

              * @see java.lang.Runnable#run()

              */

             @Override

             publicvoid run() {

                       //TODO Auto-generated method stub

                       if(type.equals("read")){

                       lock.readLock().lock();

                       }else{

                                lock.writeLock().lock();

                       }

                       System.out.println(Thread.currentThread().getName()+"获取到 读锁,休息10秒");

                       try{

                                Thread.sleep(10000);

                       }catch (InterruptedException e) {

                                //TODO Auto-generated catch block

                                e.printStackTrace();

                       }

                       if(type.equals("read")){

                                lock.readLock().unlock();

                                }else{

                                         lock.writeLock().unlock();

                                }

                       System.out.println(Thread.currentThread().getName()+"释放锁");

             }

     

             /**

             *

             *@author : zhengrf1

             *@date 创建时间:2017年3月9日上午10:19:06

             */

             publicstatic void main(String[] args) {

                       //TODO Auto-generated method stub

    //               ReadeWriteLockTesttest = new ReadeWriteLockTest();

                       ReadeWriteLockTestlock1 = new ReadeWriteLockTest();

                       ReadeWriteLockTestlock2 = new ReadeWriteLockTest();

                       lock1.type="read";

                       lock2.type="write";

                       Threadtest1  = new Thread(lock1);

                       Threadtest2  = new Thread(lock2);

                       test1.start();

                       test2.start();

             }

     

    }

    输出:

    Thread-1获取到读锁,休息10秒

    Thread-1释放锁

    Thread-0获取到读锁,休息10秒

    Thread-0释放锁

    --说明读写互斥

    转载请注明原文地址: https://ju.6miu.com/read-9916.html

    最新回复(0)