Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -14,3 +14,9 @@
|
|||||||
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
|
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
|
||||||
|:---|:---|:---|:---|:--|
|
|:---|:---|:---|:---|:--|
|
||||||
| 1 | A | 基本 api 定义 | 2020-9-2 14:45:40 | |
|
| 1 | A | 基本 api 定义 | 2020-9-2 14:45:40 | |
|
||||||
|
|
||||||
|
# release_0.0.2
|
||||||
|
|
||||||
|
| 序号 | 变更类型 | 说明 | 时间 | 备注 |
|
||||||
|
|:---|:---|:---|:---|:--|
|
||||||
|
| 1 | A | 常见锁添加 | 2020-9-2 14:45:40 | |
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>lock</artifactId>
|
<artifactId>lock</artifactId>
|
||||||
<groupId>com.github.houbb</groupId>
|
<groupId>com.github.houbb</groupId>
|
||||||
<version>0.0.2-SNAPSHOT</version>
|
<version>0.0.3-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,32 @@
|
|||||||
package com.github.houbb.lock.api.core;
|
package com.github.houbb.lock.api.core;
|
||||||
|
|
||||||
import java.util.concurrent.locks.ReadWriteLock;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读写锁定义
|
* 读写锁定义接口
|
||||||
* @author binbin.hou
|
* @author binbin.hou
|
||||||
* @since 0.0.2
|
* @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();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>lock</artifactId>
|
<artifactId>lock</artifactId>
|
||||||
<groupId>com.github.houbb</groupId>
|
<groupId>com.github.houbb</groupId>
|
||||||
<version>0.0.2-SNAPSHOT</version>
|
<version>0.0.3-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.github.houbb.lock.redis.core;
|
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.Log;
|
||||||
import com.github.houbb.log.integration.core.LogFactory;
|
import com.github.houbb.log.integration.core.LogFactory;
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ import com.github.houbb.log.integration.core.LogFactory;
|
|||||||
* @author binbin.hou
|
* @author binbin.hou
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public class LockReadWrite {
|
public class LockReadWrite implements IReadWriteLock {
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(LockReadWrite.class);
|
private static final Log log = LogFactory.getLog(LockReadWrite.class);
|
||||||
|
|
||||||
@@ -28,13 +29,19 @@ public class LockReadWrite {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public synchronized void lockRead() throws InterruptedException {
|
@Override
|
||||||
|
public synchronized void lockRead() {
|
||||||
|
try {
|
||||||
// 写锁存在,需要wait
|
// 写锁存在,需要wait
|
||||||
while (!tryLockRead()) {
|
while (!tryLockRead()) {
|
||||||
wait();
|
wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
readCount++;
|
readCount++;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.interrupted();
|
||||||
|
// 忽略打断
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,6 +64,7 @@ public class LockReadWrite {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public synchronized void unlockRead() {
|
public synchronized void unlockRead() {
|
||||||
readCount--;
|
readCount--;
|
||||||
notifyAll();
|
notifyAll();
|
||||||
@@ -67,7 +75,9 @@ public class LockReadWrite {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public synchronized void lockWrite() throws InterruptedException {
|
@Override
|
||||||
|
public synchronized void lockWrite() {
|
||||||
|
try {
|
||||||
// 写锁存在,需要wait
|
// 写锁存在,需要wait
|
||||||
while (!tryLockWrite()) {
|
while (!tryLockWrite()) {
|
||||||
wait();
|
wait();
|
||||||
@@ -75,6 +85,9 @@ public class LockReadWrite {
|
|||||||
|
|
||||||
// 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿
|
// 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿
|
||||||
writeCount++;
|
writeCount++;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.interrupted();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -103,6 +116,7 @@ public class LockReadWrite {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public synchronized void unlockWrite() {
|
public synchronized void unlockWrite() {
|
||||||
writeCount--;
|
writeCount--;
|
||||||
notifyAll();
|
notifyAll();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.github.houbb.lock.redis.core;
|
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.Log;
|
||||||
import com.github.houbb.log.integration.core.LogFactory;
|
import com.github.houbb.log.integration.core.LogFactory;
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
* @author binbin.hou
|
* @author binbin.hou
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public class LockReadWriteOwner {
|
public class LockReadWriteOwner implements IReadWriteLock {
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(LockReadWriteOwner.class);
|
private static final Log log = LogFactory.getLog(LockReadWriteOwner.class);
|
||||||
|
|
||||||
@@ -42,12 +43,17 @@ public class LockReadWriteOwner {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public synchronized void lockRead() throws InterruptedException {
|
@Override
|
||||||
|
public synchronized void lockRead() {
|
||||||
|
try {
|
||||||
// 写锁存在,需要wait
|
// 写锁存在,需要wait
|
||||||
while (!tryLockRead()) {
|
while (!tryLockRead()) {
|
||||||
log.debug("获取读锁失败,进入等待状态。");
|
log.debug("获取读锁失败,进入等待状态。");
|
||||||
wait();
|
wait();
|
||||||
}
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.interrupted();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,6 +81,7 @@ public class LockReadWriteOwner {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public synchronized void unlockRead() {
|
public synchronized void unlockRead() {
|
||||||
Thread currentThread = Thread.currentThread();
|
Thread currentThread = Thread.currentThread();
|
||||||
Integer readCount = readCountMap.get(currentThread);
|
Integer readCount = readCountMap.get(currentThread);
|
||||||
@@ -83,6 +90,7 @@ public class LockReadWriteOwner {
|
|||||||
throw new RuntimeException("当前线程未持有任何读锁,释放锁失败!");
|
throw new RuntimeException("当前线程未持有任何读锁,释放锁失败!");
|
||||||
} else {
|
} else {
|
||||||
log.debug("释放读锁,唤醒所有等待线程。");
|
log.debug("释放读锁,唤醒所有等待线程。");
|
||||||
|
readCountMap.remove(currentThread);
|
||||||
notifyAll();
|
notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,7 +100,9 @@ public class LockReadWriteOwner {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public synchronized void lockWrite() throws InterruptedException {
|
@Override
|
||||||
|
public synchronized void lockWrite() {
|
||||||
|
try {
|
||||||
// 写锁存在,需要wait
|
// 写锁存在,需要wait
|
||||||
while (!tryLockWrite()) {
|
while (!tryLockWrite()) {
|
||||||
wait();
|
wait();
|
||||||
@@ -100,6 +110,9 @@ public class LockReadWriteOwner {
|
|||||||
|
|
||||||
// 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿
|
// 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿
|
||||||
writeCount++;
|
writeCount++;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.interrupted();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -131,6 +144,7 @@ public class LockReadWriteOwner {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public synchronized void unlockWrite() {
|
public synchronized void unlockWrite() {
|
||||||
boolean toNullResult = writeOwner.compareAndSet(Thread.currentThread(), null);
|
boolean toNullResult = writeOwner.compareAndSet(Thread.currentThread(), null);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.github.houbb.lock.redis.core;
|
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.Log;
|
||||||
import com.github.houbb.log.integration.core.LogFactory;
|
import com.github.houbb.log.integration.core.LogFactory;
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ import java.util.concurrent.atomic.AtomicReference;
|
|||||||
* @author binbin.hou
|
* @author binbin.hou
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public class LockReadWriteRe {
|
public class LockReadWriteRe implements IReadWriteLock {
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(LockReadWriteRe.class);
|
private static final Log log = LogFactory.getLog(LockReadWriteRe.class);
|
||||||
|
|
||||||
@@ -42,12 +43,17 @@ public class LockReadWriteRe {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public synchronized void lockRead() throws InterruptedException {
|
@Override
|
||||||
|
public synchronized void lockRead() {
|
||||||
|
try {
|
||||||
// 写锁存在,需要wait
|
// 写锁存在,需要wait
|
||||||
while (!tryLockRead()) {
|
while (!tryLockRead()) {
|
||||||
log.debug("获取读锁失败,进入等待状态。");
|
log.debug("获取读锁失败,进入等待状态。");
|
||||||
wait();
|
wait();
|
||||||
}
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.interrupted();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,6 +86,7 @@ public class LockReadWriteRe {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public synchronized void unlockRead() {
|
public synchronized void unlockRead() {
|
||||||
Thread currentThread = Thread.currentThread();
|
Thread currentThread = Thread.currentThread();
|
||||||
Integer readCount = readCountMap.get(currentThread);
|
Integer readCount = readCountMap.get(currentThread);
|
||||||
@@ -106,7 +113,9 @@ public class LockReadWriteRe {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
public synchronized void lockWrite() throws InterruptedException {
|
@Override
|
||||||
|
public synchronized void lockWrite() {
|
||||||
|
try {
|
||||||
// 写锁存在,需要wait
|
// 写锁存在,需要wait
|
||||||
while (!tryLockWrite()) {
|
while (!tryLockWrite()) {
|
||||||
log.debug("获取写锁失败,进入等待状态。");
|
log.debug("获取写锁失败,进入等待状态。");
|
||||||
@@ -115,6 +124,9 @@ public class LockReadWriteRe {
|
|||||||
|
|
||||||
// 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿
|
// 此时已经不存在获取写锁的线程了,因此占坑,防止写锁饥饿
|
||||||
writeCount++;
|
writeCount++;
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.interrupted();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -152,6 +164,7 @@ public class LockReadWriteRe {
|
|||||||
*
|
*
|
||||||
* @since 0.0.2
|
* @since 0.0.2
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public synchronized void unlockWrite() {
|
public synchronized void unlockWrite() {
|
||||||
Thread currentThread = Thread.currentThread();
|
Thread currentThread = Thread.currentThread();
|
||||||
// 多次重入释放(当次数多于1时直接返回,否则需要释放 owner 信息)
|
// 多次重入释放(当次数多于1时直接返回,否则需要释放 owner 信息)
|
||||||
@@ -173,7 +186,7 @@ public class LockReadWriteRe {
|
|||||||
/**
|
/**
|
||||||
* 释放写锁并且通知
|
* 释放写锁并且通知
|
||||||
*/
|
*/
|
||||||
private void unlockWriteNotify() {
|
private synchronized void unlockWriteNotify() {
|
||||||
writeCount--;
|
writeCount--;
|
||||||
log.debug("释放写锁成功,唤醒所有等待线程。");
|
log.debug("释放写锁成功,唤醒所有等待线程。");
|
||||||
notifyAll();
|
notifyAll();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>lock</artifactId>
|
<artifactId>lock</artifactId>
|
||||||
<groupId>com.github.houbb</groupId>
|
<groupId>com.github.houbb</groupId>
|
||||||
<version>0.0.2-SNAPSHOT</version>
|
<version>0.0.3-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
<groupId>com.github.houbb</groupId>
|
<groupId>com.github.houbb</groupId>
|
||||||
<artifactId>lock</artifactId>
|
<artifactId>lock</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>0.0.2-SNAPSHOT</version>
|
<version>0.0.3-SNAPSHOT</version>
|
||||||
<modules>
|
<modules>
|
||||||
<module>lock-api</module>
|
<module>lock-api</module>
|
||||||
<module>lock-core</module>
|
<module>lock-core</module>
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ ECHO "============================= RELEASE START..."
|
|||||||
|
|
||||||
:: 版本号信息(需要手动指定)
|
:: 版本号信息(需要手动指定)
|
||||||
:::: 旧版本名称
|
:::: 旧版本名称
|
||||||
SET version=0.0.1
|
SET version=0.0.2
|
||||||
:::: 新版本名称
|
:::: 新版本名称
|
||||||
SET newVersion=0.0.2
|
SET newVersion=0.0.3
|
||||||
:::: 组织名称
|
:::: 组织名称
|
||||||
SET groupName=com.github.houbb
|
SET groupName=com.github.houbb
|
||||||
:::: 项目名称
|
:::: 项目名称
|
||||||
|
|||||||
Reference in New Issue
Block a user