本文共 9803 字,大约阅读时间需要 32 分钟。
ReadWriteLock
package com.hqq.concurrency.read_write_lock;import java.util.HashSet;import java.util.Set;import java.util.concurrent.TimeUnit;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReadWriteLock;/** * ReaderWriterLock * 读写锁 * 控制读写权限 * 允许多个读操作持有锁 但是如果加了写锁 读操作便会阻塞 * 如果加了读锁 写操作就会阻塞 * 是非公平锁 * Created by heqianqian on 2017/7/30. */public class ReaderWriterLock implements ReadWriteLock { private Object readerMutex = new Object(); private int currentReaderCount; private ReadLock readLock = new ReadLock(); private WriteLock writeLock = new WriteLock(); /** * 全局互斥变量 用来表示写操作和读操作是否同时获得锁 ** 1.如果包含{@link readLock},表示当前线程持有读锁,其他线程仍然可以进行读操作 * 2.如果包含{@link writeLock},表示当前线程持有写锁,其他线程既无法执行写操作也无法执行读操作 */ private Set
Reader
package com.hqq.concurrency.read_write_lock;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.concurrent.locks.Lock;/** * Reader * Created by heqianqian on 2017/7/30. */public class Reader extends Thread { private static final Logger LOGGER = LoggerFactory.getLogger(Reader.class); private String name; private Lock readLock; public Reader(String name, Lock readLock) { this.name = name; this.readLock = readLock; } @Override public void run() { readLock.lock(); try { read(); } catch (InterruptedException e) { e.printStackTrace(); } finally { readLock.unlock(); } } private void read() throws InterruptedException { LOGGER.info("{} read begin", name); Thread.sleep(500); LOGGER.info("{} read end", name); }}
Writer
package com.hqq.concurrency.read_write_lock;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.concurrent.locks.Lock;/** * Writer * Created by heqianqian on 2017/7/30. */public class Writer extends Thread { private static final Logger LOGGER = LoggerFactory.getLogger(Writer.class); private String name; private Lock writeLock; public Writer(String name, Lock writeLock) { this.name = name; this.writeLock = writeLock; } @Override public void run() { writeLock.lock(); try { write(); } catch (InterruptedException e) { e.printStackTrace(); } finally { writeLock.unlock(); } } public void write() throws InterruptedException { LOGGER.info("{} write begin", name); Thread.sleep(500); LOGGER.info("{} write end", name); }}
APP
package com.hqq.concurrency.read_write_lock;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.stream.IntStream;/** * ReadWriteLock读写锁 * 在一个多线程的环境下,不管是读还是写操作会尝试去同步共享资源. * 这会导致低性能,特别在读多于写的情况下,因为读操作对另一个线程来说的线程安全的 ** 这种模式允许同步的读操作,但是写操作会加排他锁. * 意味着多线程可以进行读操作,但是修改数据的时候需要加排他锁 * 当一个线程在写数据时,其他读或写线程会一直阻塞到当前线程完成写操作 * Created by heqianqian on 2017/7/30. */public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class); public static void main(String[] args) { ExecutorService executorService = Executors.newFixedThreadPool(10); ReaderWriterLock readerWriterLock = new ReaderWriterLock(); //开启5个读线程 IntStream.range(0, 5) .forEach(i -> executorService.submit(new Reader("Reader" + i, readerWriterLock.readLock()))); //开启5个写线程 IntStream.range(0, 5) .forEach(i -> executorService.submit(new Writer("Writer" + i, readerWriterLock.writeLock()))); executorService.shutdown(); try { executorService.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException e) { LOGGER.error("Error waiting for ExecutorService shutdown"); } }}
测试结果:
INFO [2017-08-09 02:24:34,339] com.hqq.concurrency.read_write_lock.Reader: Reader2 read beginINFO [2017-08-09 02:24:34,336] com.hqq.concurrency.read_write_lock.Reader: Reader0 read beginINFO [2017-08-09 02:24:34,337] com.hqq.concurrency.read_write_lock.Reader: Reader1 read beginINFO [2017-08-09 02:24:34,352] com.hqq.concurrency.read_write_lock.Reader: Reader3 read beginINFO [2017-08-09 02:24:34,361] com.hqq.concurrency.read_write_lock.Reader: Reader4 read beginINFO [2017-08-09 02:24:34,849] com.hqq.concurrency.read_write_lock.Reader: Reader0 read endINFO [2017-08-09 02:24:34,849] com.hqq.concurrency.read_write_lock.Reader: Reader1 read endINFO [2017-08-09 02:24:34,849] com.hqq.concurrency.read_write_lock.Reader: Reader2 read endINFO [2017-08-09 02:24:34,865] com.hqq.concurrency.read_write_lock.Reader: Reader3 read endINFO [2017-08-09 02:24:34,866] com.hqq.concurrency.read_write_lock.Reader: Reader4 read endINFO [2017-08-09 02:24:34,866] com.hqq.concurrency.read_write_lock.Writer: Writer4 write beginINFO [2017-08-09 02:24:35,402] com.hqq.concurrency.read_write_lock.Writer: Writer4 write endINFO [2017-08-09 02:24:35,402] com.hqq.concurrency.read_write_lock.Writer: Writer0 write beginINFO [2017-08-09 02:24:35,904] com.hqq.concurrency.read_write_lock.Writer: Writer0 write endINFO [2017-08-09 02:24:35,904] com.hqq.concurrency.read_write_lock.Writer: Writer3 write beginINFO [2017-08-09 02:24:36,410] com.hqq.concurrency.read_write_lock.Writer: Writer3 write endINFO [2017-08-09 02:24:36,411] com.hqq.concurrency.read_write_lock.Writer: Writer1 write beginINFO [2017-08-09 02:24:36,911] com.hqq.concurrency.read_write_lock.Writer: Writer1 write endINFO [2017-08-09 02:24:36,912] com.hqq.concurrency.read_write_lock.Writer: Writer2 write beginINFO [2017-08-09 02:24:37,415] com.hqq.concurrency.read_write_lock.Writer: Writer2 write end