线程:ReentrantReadWriteLock类

ReentrantReadWriteLock类为读写锁。
读写锁也有两个锁,一个是读操作相关的锁,称为共享锁;
另一个是写操作相关的锁,也叫排他锁。
也就是多个读锁之间不互斥,读锁与写锁互斥,写锁与写锁互斥。


一个线程拥有了对象A的写锁,在释放写锁前其他线程无法获得A的读锁、写锁,因此其他线程此时无法读写;
一个线程拥有了对象A的读锁,在释放前其他线程可以获得A的读锁但无法获得A的写锁,因此其他线程此时可以读不可以写。
不加读锁的话其他线程是可以读,但也可以写,这时就可能导致数据不一致了

 

读锁共享

   使用lock.readLock()读锁可以提高程序运行效率,允许多个线程同时执行lock()方法后面的代码

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Service {
    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 class ThreadA extends Thread{
    private Service service = new Service();

    public ThreadA(Service service){
        super();
        this.service = service;
    }


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


public class ThreadB extends Thread{
    private Service service = new Service();

    public ThreadB(Service service){
        super();
        this.service = service;
    }


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


public class Run {
    public static void main(String[] args) {
        Service service = new Service();
        ThreadA a = new ThreadA(service);
        ThreadB b = new ThreadB(service);
        a.setName("A");
        b.setName("B");
        a.start();
        b.start();
    }
}

 

写锁互斥

  使用写锁代码lock.writeLock()的效果就是同一时间只允许一个线程执行lock()方法后面的代码

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Service {
	private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
	
	public void writeLock(){
		try {
			lock.writeLock().lock();
			System.out.println("进入写锁:"+Thread.currentThread().getName()+" 时间:"+System.currentTimeMillis());
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally{
			lock.writeLock().unlock();
		}
	}
}

public class ThreadA extends Thread{
	private Service service;
	
	public ThreadA(Service service){
		super();
		this.service = service;
	}
	
	@Override
	public void run(){
		service.writeLock();
	}
}

public class ThreadB extends Thread{
	private Service service;
	
	public ThreadB(Service service){
		super();
		this.service = service;
	}
	
	@Override
	public void run(){
		service.writeLock();
	}
}

public class Run {
	public static void main(String[] args){
		Service s = new Service();
		ThreadA a = new ThreadA(s);
		ThreadB b = new ThreadB(s);
		
		a.setName("A");
		b.setName("B");
		a.start();
		b.start();
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值