Flask接口签名sign原理与实例代码浅析
266
2023-02-17
Java语言ReadWriteLock特性实例测试
本文研究的主要是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 = fkFOwuVAplock.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() {fkFOwuVAp
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.79fkFOwuVAp5
Thread[Thread-0,5,main] write:end 2018-01-22 13:54:26.795
测试结果解读:
同时启动了3个线程,其中1号线程Thread[Thread-0,5,main],执行fkFOwuVApwrite时先休眠了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特性实例测试的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~