服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - Java语言ReadWriteLock特性实例测试

Java语言ReadWriteLock特性实例测试

2021-04-05 12:23Alioo-壞人 JAVA教程

这篇文章主要介绍了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释放锁之后才可以拿到

测试代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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]将会优先执行代码

?
1
read.lock();

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

?
1
2
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秒),所以也将同时执行结束

?
1
2
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并执行自己的业务代码

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

原文链接:http://blog.csdn.net/hl_java/article/details/79128667

延伸 · 阅读

精彩推荐