Java语言ReadWriteLock特性实例测试

本文研究的主要是ReadWriteLock特性,具体如下。

成都创新互联是一家专业提供合江企业网站建设,专注与成都网站设计、网站制作H5高端网站建设、小程序制作等业务。10年已为合江众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。

ReadWriteLock几点特性

readLock 与 readLock 不互斥
readLock 与 writeLock 互斥
writeLock 与 readLock 互斥
writeLock 与 writeLock 互斥

举例来说:

线程1, 先拿到readLock, 线程2试图拿readLock, 可以拿到
线程1, 先拿到readLock, 线程2试图拿writeLock, 阻塞等待,直到线程1释放锁之后才可以拿到
线程1, 先拿到writeLock,线程2试图拿readLock, 阻塞等待,直到线程1释放锁之后才可以拿到
线程1, 先拿到writeLock,线程2试图拿writeLock, 阻塞等待,直到线程1释放锁之后才可以拿到

测试代码

package com.alioo.lock;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
 *
 */
public class ReadWriteLockDemo {
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
	public static void main(String[] args) {
		Data data = new Data();
		Worker t1 = new Worker(data, false);
		//写
		Worker t2 = new Worker(data, true);
		//读
		Worker t3 = new Worker(data, true);
		//读
		t1.start();
		t2.start();
		t3.start();
	}
	static class Worker extends Thread {
		Data data;
		Boolean read;
		public Worker(Data data, Boolean read) {
			this.data = data;
			this.read = read;
		}
		public void run() {
			if (read)
			    data.read(); else
			    data.write();
		}
	}
	static class Data {
		ReadWriteLock lock = new ReentrantReadWriteLock();
		Lock read = lock.readLock();
		Lock write = lock.writeLock();
		public void write() {
			try {
				Thread.sleep(2000);
				//
			}
			catch (Exception e) {
			}
			write.lock();
			System.out.println(Thread.currentThread() + " write:begin "
			     + sdf.format(new Date()));
			try {
				Thread.sleep(5000);
				//
			}
			catch (Exception e) {
			}
			finally {
				System.out.println(Thread.currentThread() + " write:end "
				      + sdf.format(new Date()));
				write.unlock();
			}
		}
		public int read() {
			read.lock();
			System.out.println(Thread.currentThread()+ " read :begin "
			     + sdf.format(new Date()));
			try {
				Thread.sleep(5000);
				//
			}
			catch (Exception e) {
			}
			finally {
				System.out.println(Thread.currentThread() + " read :end "
				      + sdf.format(new Date()));
				read.unlock();
			}
			return 1;
		}
	}
}

测试结果:

Thread[Thread-2,5,main] read :begin 2018-01-22 13:54:16.794
Thread[Thread-1,5,main] read :begin 2018-01-22 13:54:16.794
Thread[Thread-2,5,main] read :end 2018-01-22 13:54:21.795
Thread[Thread-1,5,main] read :end 2018-01-22 13:54:21.795
Thread[Thread-0,5,main] write:begin 2018-01-22 13:54:21.795
Thread[Thread-0,5,main] write:end 2018-01-22 13:54:26.795

测试结果解读:

同时启动了3个线程,其中1号线程Thread[Thread-0,5,main],执行write时先休眠了2秒.那么 2,3号线程Thread[Thread-1,5,main],Thread[Thread-2,5,main]将会优先执行代码

read.lock();

由于read.lock();是不互斥的(即可重入的),所以他们同时拿到了锁,通过日志可以看出来

Thread[Thread-2,5,main] read :begin 2018-01-22 13:54:16.794
Thread[Thread-1,5,main] read :begin 2018-01-22 13:54:16.794

而且他们执行的时间开销是相同的(测试代码中都是休眠5秒),所以也将同时执行结束

Thread[Thread-2,5,main] read :end 2018-01-22 13:54:21.795
Thread[Thread-1,5,main] read :end 2018-01-22 13:54:21.795

只有当所有的readLock被释放掉之后,writeLock才能拿到锁,而这个时候Thread[Thread-1,5,main],Thread[Thread-2,5,main]执行结束后就会释放锁readLock

所以Thread[Thread-0,5,main]这个时候拿到了writeLock并执行自己的业务代码

Thread[Thread-0,5,main] write:begin 2018-01-22 13:54:21.795
Thread[Thread-0,5,main] write:end 2018-01-22 13:54:26.795

总结

以上就是本文关于Java语言ReadWriteLock特性实例测试的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!


网页题目:Java语言ReadWriteLock特性实例测试
网站URL:http://myzitong.com/article/jdjhee.html