From 2cc9eccdeec0c82c2d144ac2781e2612f4f2db11 Mon Sep 17 00:00:00 2001 From: "binbin.hou" Date: Sat, 24 Oct 2020 20:21:11 +0800 Subject: [PATCH] [Feature] add for new --- .../houbb/lock/api/core/IReadWriteLock.java | 29 +++++++++++-- .../houbb/lock/redis/core/LockReadWrite.java | 42 ++++++++++++------ .../lock/redis/core/LockReadWriteOwner.java | 40 +++++++++++------ .../lock/redis/core/LockReadWriteRe.java | 43 ++++++++++++------- 4 files changed, 108 insertions(+), 46 deletions(-) diff --git a/lock-api/src/main/java/com/github/houbb/lock/api/core/IReadWriteLock.java b/lock-api/src/main/java/com/github/houbb/lock/api/core/IReadWriteLock.java index eb56167..b1ff26c 100644 --- a/lock-api/src/main/java/com/github/houbb/lock/api/core/IReadWriteLock.java +++ b/lock-api/src/main/java/com/github/houbb/lock/api/core/IReadWriteLock.java @@ -1,11 +1,32 @@ package com.github.houbb.lock.api.core; -import java.util.concurrent.locks.ReadWriteLock; - /** - * 读写锁定义 + * 读写锁定义接口 * @author binbin.hou * @since 0.0.2 */ -public interface IReadWriteLock extends ReadWriteLock { +public interface IReadWriteLock { + + /** + * 获取读锁 + * @since 0.0.2 + */ + void lockRead(); + + /** + * 释放读锁 + */ + void unlockRead(); + + /** + * 获取写锁 + * @since 0.0.2 + */ + void lockWrite(); + + /** + * 释放写锁 + */ + void unlockWrite(); + } diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWrite.java b/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWrite.java index b913dfc..cba4717 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWrite.java +++ b/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWrite.java @@ -1,5 +1,6 @@ package com.github.houbb.lock.redis.core; +import com.github.houbb.lock.api.core.IReadWriteLock; import com.github.houbb.log.integration.core.Log; import com.github.houbb.log.integration.core.LogFactory; @@ -9,7 +10,7 @@ import com.github.houbb.log.integration.core.LogFactory; * @author binbin.hou * @since 0.0.2 */ -public class LockReadWrite { +public class LockReadWrite implements IReadWriteLock { private static final Log log = LogFactory.getLog(LockReadWrite.class); @@ -28,13 +29,19 @@ public class LockReadWrite { * * @since 0.0.2 */ - public synchronized void lockRead() throws InterruptedException { - // 写锁存在,需要wait - while (!tryLockRead()) { - wait(); - } + @Override + public synchronized void lockRead() { + try { + // 写锁存在,需要wait + while (!tryLockRead()) { + wait(); + } - readCount++; + readCount++; + } catch (InterruptedException e) { + Thread.interrupted(); + // 忽略打断 + } } /** @@ -57,6 +64,7 @@ public class LockReadWrite { * * @since 0.0.2 */ + @Override public synchronized void unlockRead() { readCount--; notifyAll(); @@ -67,14 +75,19 @@ public class LockReadWrite { * * @since 0.0.2 */ - public synchronized void lockWrite() throws InterruptedException { - // 写锁存在,需要wait - while (!tryLockWrite()) { - wait(); - } + @Override + public synchronized void lockWrite() { + try { + // 写锁存在,需要wait + while (!tryLockWrite()) { + wait(); + } - // 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿 - writeCount++; + // 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿 + writeCount++; + } catch (InterruptedException e) { + Thread.interrupted(); + } } /** @@ -103,6 +116,7 @@ public class LockReadWrite { * * @since 0.0.2 */ + @Override public synchronized void unlockWrite() { writeCount--; notifyAll(); diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteOwner.java b/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteOwner.java index f3b4980..d62c41c 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteOwner.java +++ b/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteOwner.java @@ -1,5 +1,6 @@ package com.github.houbb.lock.redis.core; +import com.github.houbb.lock.api.core.IReadWriteLock; import com.github.houbb.log.integration.core.Log; import com.github.houbb.log.integration.core.LogFactory; @@ -13,7 +14,7 @@ import java.util.concurrent.atomic.AtomicReference; * @author binbin.hou * @since 0.0.2 */ -public class LockReadWriteOwner { +public class LockReadWriteOwner implements IReadWriteLock { private static final Log log = LogFactory.getLog(LockReadWriteOwner.class); @@ -42,11 +43,16 @@ public class LockReadWriteOwner { * * @since 0.0.2 */ - public synchronized void lockRead() throws InterruptedException { - // 写锁存在,需要wait - while (!tryLockRead()) { - log.debug("获取读锁失败,进入等待状态。"); - wait(); + @Override + public synchronized void lockRead() { + try { + // 写锁存在,需要wait + while (!tryLockRead()) { + log.debug("获取读锁失败,进入等待状态。"); + wait(); + } + } catch (InterruptedException e) { + Thread.interrupted(); } } @@ -75,6 +81,7 @@ public class LockReadWriteOwner { * * @since 0.0.2 */ + @Override public synchronized void unlockRead() { Thread currentThread = Thread.currentThread(); Integer readCount = readCountMap.get(currentThread); @@ -83,6 +90,7 @@ public class LockReadWriteOwner { throw new RuntimeException("当前线程未持有任何读锁,释放锁失败!"); } else { log.debug("释放读锁,唤醒所有等待线程。"); + readCountMap.remove(currentThread); notifyAll(); } } @@ -92,14 +100,19 @@ public class LockReadWriteOwner { * * @since 0.0.2 */ - public synchronized void lockWrite() throws InterruptedException { - // 写锁存在,需要wait - while (!tryLockWrite()) { - wait(); - } + @Override + public synchronized void lockWrite() { + try { + // 写锁存在,需要wait + while (!tryLockWrite()) { + wait(); + } - // 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿 - writeCount++; + // 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿 + writeCount++; + } catch (InterruptedException e) { + Thread.interrupted(); + } } /** @@ -131,6 +144,7 @@ public class LockReadWriteOwner { * * @since 0.0.2 */ + @Override public synchronized void unlockWrite() { boolean toNullResult = writeOwner.compareAndSet(Thread.currentThread(), null); diff --git a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteRe.java b/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteRe.java index 1ce5217..10e6e56 100644 --- a/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteRe.java +++ b/lock-core/src/main/java/com/github/houbb/lock/redis/core/LockReadWriteRe.java @@ -1,5 +1,6 @@ package com.github.houbb.lock.redis.core; +import com.github.houbb.lock.api.core.IReadWriteLock; import com.github.houbb.log.integration.core.Log; import com.github.houbb.log.integration.core.LogFactory; @@ -13,7 +14,7 @@ import java.util.concurrent.atomic.AtomicReference; * @author binbin.hou * @since 0.0.2 */ -public class LockReadWriteRe { +public class LockReadWriteRe implements IReadWriteLock { private static final Log log = LogFactory.getLog(LockReadWriteRe.class); @@ -42,11 +43,16 @@ public class LockReadWriteRe { * * @since 0.0.2 */ - public synchronized void lockRead() throws InterruptedException { - // 写锁存在,需要wait - while (!tryLockRead()) { - log.debug("获取读锁失败,进入等待状态。"); - wait(); + @Override + public synchronized void lockRead() { + try { + // 写锁存在,需要wait + while (!tryLockRead()) { + log.debug("获取读锁失败,进入等待状态。"); + wait(); + } + } catch (InterruptedException e) { + Thread.interrupted(); } } @@ -80,6 +86,7 @@ public class LockReadWriteRe { * * @since 0.0.2 */ + @Override public synchronized void unlockRead() { Thread currentThread = Thread.currentThread(); Integer readCount = readCountMap.get(currentThread); @@ -106,15 +113,20 @@ public class LockReadWriteRe { * * @since 0.0.2 */ - public synchronized void lockWrite() throws InterruptedException { - // 写锁存在,需要wait - while (!tryLockWrite()) { - log.debug("获取写锁失败,进入等待状态。"); - wait(); - } + @Override + public synchronized void lockWrite() { + try { + // 写锁存在,需要wait + while (!tryLockWrite()) { + log.debug("获取写锁失败,进入等待状态。"); + wait(); + } - // 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿 - writeCount++; + // 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿 + writeCount++; + } catch (InterruptedException e) { + Thread.interrupted(); + } } /** @@ -152,6 +164,7 @@ public class LockReadWriteRe { * * @since 0.0.2 */ + @Override public synchronized void unlockWrite() { Thread currentThread = Thread.currentThread(); // 多次重入释放(当次数多于1时直接返回,否则需要释放 owner 信息) @@ -173,7 +186,7 @@ public class LockReadWriteRe { /** * 释放写锁并且通知 */ - private void unlockWriteNotify() { + private synchronized void unlockWriteNotify() { writeCount--; log.debug("释放写锁成功,唤醒所有等待线程。"); notifyAll();