java顺序读写和随机读写代码(Java手写一个读写锁)

读写锁设计的思想是:读操作可以同时进行,读的时候不能写,写的时候不能读,写的时候不能写,今天小编就来聊一聊关于java顺序读写和随机读写代码?接下来我们就一起去研究一下吧!

java顺序读写和随机读写代码(Java手写一个读写锁)

java顺序读写和随机读写代码

读写锁设计的思想是:读操作可以同时进行,读的时候不能写,写的时候不能读,写的时候不能写。

这样做是为了保护共享数据的安全性,不会发生脏数据。

//读写锁核心类 public class ReadWritelock { //当前有多少个线程正在读 private int readingReaders = 0; //当前有多少个线程正在等待读 private int waitingReaders = 0; //当前有多少个线程正在写 private int writingWriters = 0; //当前有多少个线程正在等待写 private int waitingWriters = 0; //是否写操作优先 private boolean preferWrite = false; public ReadWriteLock() { this(false); } public ReadWriteLock(boolean preferWrite) { this.preferWrite = preferWrite; } //读锁 public synchronized void readLock() throws InterruptedException { //刚拿到锁以后是等待读取 this.waitingReaders ; try { //如果正在有线程在写操作或者写操作更优先时,则读操作进入等待队列等待 while (this.writingWriters > 0 || (preferWrite && waitingWriters > 0)) { this.wait(); } //正在进行读操作 this.readingReaders ; } finally { //已经开始读取,所以等待读取减 this.waitingReaders--; } } //释放读锁 public synchronized void unReadLock() throws InterruptedException { this.readingReaders--; this.notifyAll(); } //写锁 public synchronized void writeLock() throws InterruptedException { //刚拿到锁以后是等待写 this.waitingWriters ; try { //如果正在有线程在写操作或者是读操作,则进入等待队列等待 while (this.writingWriters > 0 || this.readingReaders > 0) { this.wait(); } //正在进行写操作 this.writingWriters ; } finally { //已经开始写,所以等待写取减 this.waitingWriters--; } } //释放写锁 public synchronized void unWriteLock() throws InterruptedException { this.writingWriters--; this.notifyAll(); } }

//共享数据 public class SharedData { //定义一个缓冲数组 private final char[] buffer; //定义一个读写锁 private final ReadWriteLock lock = new ReadWriteLock(); public SharedData(int size) { this.buffer = new char[size]; for (int i = 0; i < size; i ) { buffer[i] = '*'; } } //读 public char[] read() throws InterruptedException { try { lock.readLock(); return doRead(); } finally { lock.unReadLock(); } } //写 public void write(char c) throws InterruptedException { try { lock.writeLock(); this.doWrite(c); } finally { lock.unWriteLock(); } } private void doWrite(char c) { for (int i = 0; i < buffer.length; i ) { buffer[i] = c; slowly(10); } System.out.println(Thread.currentThread().getName() " write " String.valueOf(c)); } private char[] doRead() { char[] newChar = new char[buffer.length]; for (int i = 0; i < buffer.length; i ) { newChar[i] = buffer[i]; } slowly(500); return newChar; } private void slowly(int ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { e.printStackTrace(); } } }

public class WriteWorker extends Thread { private static final Random RANDOM = new Random(System.currentTimeMillis()); private final SharedData data; private final String filler; private int index = 0; public WriteWorker(SharedData data, String filler) { this.data = data; this.filler = filler; } @Override public void run() { try { while (true) { char c = nextChar(); data.write(c); Thread.sleep(RANDOM.nextInt(1000)); } } catch (InterruptedException e) { e.printStackTrace(); } } private char nextChar() { char c = filler.charAt(index); index ; if (index > filler.length()) { index = 0; } return c; } }

public class ReadWorker extends Thread { private final SharedData data; public ReadWorker(SharedData data) { this.data = data; } @Override public void run() { try { while (true) { char[] read = data.read(); System.out.println(Thread.currentThread().getName() " read " String.valueOf(read)); } } catch (InterruptedException e) { e.printStackTrace(); } } }

public class ReadWriteLockClient { public static void main(String[] args) { final SharedData data = new SharedData(10); new ReadWorker(data).start(); new ReadWorker(data).start(); new ReadWorker(data).start(); new ReadWorker(data).start(); new ReadWorker(data).start(); new WriteWorker(data,"dasdfdfdsfssfd").start(); new WriteWorker(data,"fghbyteyjyjyjyj").start(); } }

Thread-3 read ********** Thread-2 read ********** Thread-1 read ********** Thread-4 read ********** Thread-0 read ********** Thread-6 write f Thread-3 read ffffffffff Thread-0 read ffffffffff Thread-2 read ffffffffff Thread-4 read ffffffffff Thread-1 read ffffffffff Thread-5 write d Thread-3 read dddddddddd Thread-2 read dddddddddd Thread-1 read dddddddddd Thread-4 read dddddddddd Thread-0 read dddddddddd Thread-6 write g Thread-3 read gggggggggg Thread-2 read gggggggggg Thread-0 read gggggggggg Thread-4 read gggggggggg Thread-1 read gggggggggg Thread-5 write a Thread-3 read aaaaaaaaaa Thread-2 read aaaaaaaaaa Thread-1 read aaaaaaaaaa Thread-0 read aaaaaaaaaa Thread-4 read aaaaaaaaaa Thread-5 write s Thread-2 read ssssssssss Thread-0 read ssssssssss Thread-4 read ssssssssss Thread-1 read ssssssssss Thread-3 read ssssssssss Thread-5 write d

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页