欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java语言ReadWriteLock特性实例测试

程序员文章站 2022-06-05 08:42:08
本文研究的主要是readwritelock特性,具体如下。 readwritelock几点特性 readlock 与 readlock 不互斥 readlock...

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

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特性实例测试的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!