多线程编程之ReentrantReadWriteLock

ReentrantLock是一个完全互斥排他的效果(和synchronized一样),但是这样有的时候会影响效率,就像两个线程同时读取一个数据,这样并不会产生线程不安全,如果使用ReentrantLock则会形成:先读线程一,然后读线程二,但是使用ReentrantReadWriteLock就可以避免这种情况。

读读共享:

package ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
//读读共享
public class Service {
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    public void Method() {
        try {
            lock.readLock().lock();
            System.out.println("线程"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"获得线程");
            Thread.sleep(10000);

        }catch(InterruptedException e){
            e.printStackTrace();

        }finally {
            lock.readLock().unlock();
        }
    }

}

package ReentrantReadWriteLock;

public class Run {
    public static void main(String[] args) {
        Service service = new Service();
        Runnable runnable = new Runnable() {
            public void run() {
                service.Method();
            }

        };
        Thread threadA = new Thread(runnable);
        Thread threadB = new Thread(runnable);
        threadA.setName("A");
        threadA.start();
        threadB.setName("B");
        threadB.start();
    }



}
//输出:
//线程B在1508554977877获得锁
//线程A在1508554977877获得锁
//几乎同一时间开始

写写互斥:

package ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
//写写互斥
public class Service2 {
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    public void Method() {
        try {
            lock.writeLock().lock();
            System.out.println("线程"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"获得线程");
            Thread.sleep(10000);

        }catch(InterruptedException e){
            e.printStackTrace();

        }finally {
            lock.writeLock().unlock();
        }
    }

}

package ReentrantReadWriteLock;
//写写互斥
public class Run2 {
    public static void main(String[] args) {
        Service2 service = new Service2();
        Runnable runnable = new Runnable() {
            public void run() {
                service.Method();
            }

        };
        Thread threadA = new Thread(runnable);
        Thread threadB = new Thread(runnable);
        threadA.setName("A");
        threadA.start();
        threadB.setName("B");
        threadB.start();
    }



}
//输出:
//线程A在1508555054030获得线程
//线程B在1508555064030获得线程
//这个大约中间间隔了10秒钟

读写互斥:

package ReentrantReadWriteLock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Service3 {
    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    public void read() {
        try {
            lock.readLock().lock();
            System.out.println("线程"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"获得锁");
            Thread.sleep(10000);

        }catch(InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.readLock().unlock();
        }
    }


    public void write() {
        try {
            lock.writeLock().lock();
            System.out.println("线程"+Thread.currentThread().getName()+"在"+System.currentTimeMillis()+"获得锁");
            Thread.sleep(10000);

        }catch(InterruptedException e) {
            e.printStackTrace();
        }finally {
            lock.writeLock().unlock();
        }

    }

}

package ReentrantReadWriteLock;

public class Run3 {
    public static void main(String[] args) {
        Service3 service = new Service3();
        Runnable runnableRead = new Runnable() {
            public void run() {
                service.read();
            }
        };
        Runnable runnablewrite = new Runnable() {
            public void run() {
                service.write();
            }
        };
        Thread threadA = new Thread(runnableRead);
        threadA.setName("A");
        threadA.start();

        Thread threadB = new Thread(runnablewrite);
        threadB.setName("B");
        threadB.start();
    }

}
//输出:
//线程A在1508555152523获得锁
//线程B在1508555162524获得锁
//中间也间隔了10秒左右
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值