package function.thread;
import java.util.Random;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class LockTest {
public static void main(String args[]){
final ShareResourceOper sro = new ShareResourceOper();
for (int i = 0; i < 3; i++) {
new Thread() {
public void run() {
while (true) {
sro.get();
}
}
}.start();//三个消费者
new Thread() {
public void run() {
while (true) {
sro.put(new Random().nextInt(10000));
}
}
}.start();//三个生产者
}
}
}
class ShareResourceOper {
private Object shareData = null;// 共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。
//读写锁
ReadWriteLock rwl = new ReentrantReadWriteLock();
// 相当于读操作
public void get() {
rwl.readLock().lock();
String threadName = Thread.currentThread().getName();
try {
System.out.println("**************** read thread "+threadName + " be ready to read data!");
Thread.sleep(1000);
if(this.shareData !=null && !this.shareData.equals("")){
System.out.println(" read thread "+threadName+ " have read data :" + shareData);
}else{
System.out.println(" read thread "+threadName+ " empty !");
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("**************** read thread "+threadName+ " release the lock !");
//可能执行完该语句,读线程就抢占了CPU,要想精准看出锁的同步排斥机制,system语句需要放在锁语句的前面
rwl.readLock().unlock();
//System.out.println("**************** read thread "+threadName+ " release the lock !");
}
}
// 相当于写操作
public void put(Object data) {
rwl.writeLock().lock();
String threadName = Thread.currentThread().getName();
try {
System.out.println("**************** write thread "+threadName+ " be ready to write data!");
Thread.sleep(1000);
this.shareData = data;
System.out.println(" write thread "+threadName+ " have write data: " + data);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("**************** write thread "+threadName+ " release the lock !");
rwl.writeLock().unlock();
}
}
}
运行结果:
**************** read thread Thread-0 be ready to read data!
**************** read thread Thread-2 be ready to read data!
**************** read thread Thread-4 be ready to read data!
read thread Thread-0 empty !
**************** read thread Thread-0 release the lock !
read thread Thread-2 empty !
**************** read thread Thread-2 release the lock !
read thread Thread-4 empty !
**************** read thread Thread-4 release the lock !
**************** write thread Thread-3 be ready to write data!
write thread Thread-3 have write data: 692
**************** write thread Thread-3 release the lock !
**************** write thread Thread-3 be ready to write data!
write thread Thread-3 have write data: 349
**************** write thread Thread-3 release the lock !
**************** write thread Thread-1 be ready to write data!
write thread Thread-1 have write data: 6981
**************** write thread Thread-1 release the lock !
**************** write thread Thread-1 be ready to write data!
write thread Thread-1 have write data: 3742
**************** write thread Thread-1 release the lock !
**************** write thread Thread-1 be ready to write data!
write thread Thread-1 have write data: 2155
**************** write thread Thread-1 release the lock !
**************** write thread Thread-5 be ready to write data!
write thread Thread-5 have write data: 6080
**************** write thread Thread-5 release the lock !
**************** write thread Thread-5 be ready to write data!
write thread Thread-5 have write data: 1327
**************** write thread Thread-5 release the lock !
**************** write thread Thread-5 be ready to write data!
write thread Thread-5 have write data: 9358
**************** write thread Thread-5 release the lock !
**************** read thread Thread-0 be ready to read data!
**************** read thread Thread-2 be ready to read data!
**************** read thread Thread-4 be ready to read data!
read thread Thread-0 have read data :9358
**************** read thread Thread-0 release the lock !
read thread Thread-4 have read data :9358
**************** read thread Thread-4 release the lock !
read thread Thread-2 have read data :9358
**************** read thread Thread-2 release the lock !
**************** write thread Thread-3 be ready to write data!
write thread Thread-3 have write data: 3529
**************** write thread Thread-3 release the lock !
**************** write thread Thread-3 be ready to write data!
write thread Thread-3 have write data: 9146
**************** write thread Thread-3 release the lock !
**************** write thread Thread-1 be ready to write data!
write thread Thread-1 have write data: 1339
**************** write thread Thread-1 release the lock !
**************** write thread Thread-1 be ready to write data!
write thread Thread-1 have write data: 4327
**************** write thread Thread-1 release the lock !
**************** write thread Thread-5 be ready to write data!
write thread Thread-5 have write data: 2020
**************** write thread Thread-5 release the lock !
**************** read thread Thread-0 be ready to read data!
**************** read thread Thread-4 be ready to read data!
**************** read thread Thread-2 be ready to read data!
read thread Thread-0 have read data :2020
**************** read thread Thread-0 release the lock !
read thread Thread-4 have read data :2020
**************** read thread Thread-4 release the lock !
read thread Thread-2 have read data :2020
**************** read thread Thread-2 release the lock !
**************** write thread Thread-3 be ready to write data!
write thread Thread-3 have write data: 633
**************** write thread Thread-3 release the lock !
**************** write thread Thread-3 be ready to write data!
write thread Thread-3 have write data: 5760
**************** write thread Thread-3 release the lock !
**************** write thread Thread-3 be ready to write data!
write thread Thread-3 have write data: 2856
**************** write thread Thread-3 release the lock !
**************** write thread Thread-3 be ready to write data!
write thread Thread-3 have write data: 1045
**************** write thread Thread-3 release the lock !
**************** write thread Thread-1 be ready to write data!
相关推荐
在Linux系统中,读写锁(Read/Write Locks,简称rwlocks)是一种多线程同步机制,它允许多个线程同时进行读操作,但只允许一个线程执行写操作。这种锁的设计目的是提高并发性能,特别是当读操作远多于写操作时。在...
在Windows操作系统中,读写锁(Read-Write Lock)是一种多线程同步原语,它允许多个线程同时读取共享资源,但在写入时仅允许一个线程访问。这提高了并发性能,尤其是在读操作远多于写操作的场景下。本篇文章将深入...
在C++中,还可以使用智能指针(如`std::unique_lock`或`std::shared_lock`)与读写锁配合,以实现RAII(Resource Acquisition Is Initialization)风格的代码,这样可以更安全地管理锁的生命周期。 总的来说,...
其中,读写锁(Read-Write Lock,简称RWLock)是一种高效的线程同步机制,适用于大量读取操作和少量写入操作的情况。在本文中,我们将深入探讨基于关键区的Windows读写锁类及其自动锁的实现原理和改进点。 读写锁的...
Java 读写锁是Java并发编程中的一种重要机制,它为多线程环境下的数据访问提供了更为精细的控制。在Java的`java.util.concurrent.locks`包中,`ReentrantReadWriteLock`类实现了读写锁的功能。这个锁允许多个读取者...
本文将讨论一种特殊的自旋锁变种,即读写锁(read-write lock),它允许多个进程同时访问共享资源,但是写操作只有一个进程可以进行。 读写锁的设计目的是为了在 Linux 内核中实现高效的数据互斥机制,特别是在...
在这个例子中,我们讨论了如何使用读写锁(Read-Write Lock)来实现多线程环境中的并发访问控制。读写锁是一种高级的同步机制,它允许多个线程同时读取共享资源,但只允许一个线程进行写操作。这在数据读取远比写入...
在C++编程中,读写锁(Read-Write Lock)是一种多线程同步机制,它允许多个线程同时读取共享资源,但只允许一个线程写入。这种锁机制提高了对共享数据的并发访问效率,是并发编程中常用的一种工具。本实例将通过代码...
读写锁是多线程编程中的一个重要概念,用于提高并发访问数据时的效率。在并发环境中,如果多个线程同时读取数据,通常不会产生冲突,而写入数据时则可能引发问题。读写锁正是为了解决这个问题,它允许多个读取线程...
读写锁(Read-Write Lock),也称为共享独占锁,是多线程编程中一种高效的数据同步机制。它允许多个读线程同时访问数据,但只允许一个写线程独占资源,确保数据的一致性。在某些情况下,这种锁能够显著提高并发性能...
读写锁是多线程编程中的一个重要概念,它在C#中被广泛应用于提高并发访问数据的效率。本文将深入探讨C#中的读写锁及其使用,帮助你理解和掌握这一关键技能。 首先,读写锁提供了对共享资源的细粒度控制。在C#中,`...
在Windows操作系统中,读写锁(Read-Write Lock)是一种多线程同步机制,它允许多个线程同时读取共享资源,但在写入时只允许一个线程进行操作。这提高了并发性能,因为读操作通常是非冲突的。本文将详细讨论如何在...
本文实例讲述了C#解决SQlite并发异常问题的方法。分享给大家供大家参考,...作者利用读写锁(ReaderWriterLock),达到了多线程安全访问的目标。 using System; using System.Collections.Generic; using System.Text;
MySQL作为一款广泛使用的开源关系型数据库管理系统,提供了多种类型的锁来满足不同场景的需求,其中读写锁(Read-Write Lock)是其中一个重要的组成部分。 读写锁允许一个资源在同一时间被多个读取者访问,但只允许...
关于读写锁算法的Java实现及思考,是一个深入探讨了多线程环境下资源访问控制机制的主题。在现代软件开发中,尤其是并发编程领域,读写锁(ReadWriteLock)是一种非常重要的同步工具,它允许多个线程同时进行读操作...
数据库读写锁是一种多线程编程中的同步机制,用于管理对共享资源的访问,以确保在任何时刻,只有一个写操作或者多个读操作可以并行进行。在C++中实现这样的锁,通常会涉及到线程安全的数据结构和低级别的内存同步...
在Linux高级程序设计中,主要介绍了三种线程同步机制:互斥锁、条件变量和读写锁,以及线程与信号的交互。 1. **互斥锁通信机制**: 互斥锁是用于保护临界区的一种机制,确保同一时间只有一个线程能访问共享资源。...
读写锁是一种多线程同步机制,用于提高对共享资源的并发访问效率。在传统的互斥锁中,只有一个线程可以访问资源,而读写锁则允许多个读线程同时访问,但当有写线程时,所有读线程和写线程都会被阻塞,确保写操作的...
为了解决这个问题,C#提供了一种名为“读写锁”(ReaderWriterLockSlim)的同步原语,它允许对共享资源进行高效的读写控制,确保了多线程的安全性。本文将深入探讨如何利用读写锁在C#中实现多线程的高效安全资源访问...