Looly 5 年之前
父节点
当前提交
7c4c6c7af1

+ 1 - 0
CHANGELOG.md

@@ -24,6 +24,7 @@
 
 ### Bug修复
 * 【core   】     修复SimpleCache死锁问题(issue#I1HOKB@Gitee)
+* 【core   】     修复SemaphoreRunnable释放问题(issue#I1HLQQ@Gitee)
 
 -------------------------------------------------------------------------------------------------------------
 

+ 17 - 5
hutool-core/src/main/java/cn/hutool/core/thread/SemaphoreRunnable.java

@@ -31,16 +31,28 @@ public class SemaphoreRunnable implements Runnable {
 		this.semaphore = semaphore;
 	}
 
+	/**
+	 * 获得信号量
+	 *
+	 * @return {@link Semaphore}
+	 * @since 5.3.6
+	 */
+	public Semaphore getSemaphore(){
+		return this.semaphore;
+	}
+
 	@Override
 	public void run() {
 		if (null != this.semaphore) {
-			try {
+			try{
 				semaphore.acquire();
-				this.runnable.run();
-			} catch (InterruptedException e) {
+				try {
+					this.runnable.run();
+				} finally {
+					semaphore.release();
+				}
+			}catch (InterruptedException e) {
 				Thread.currentThread().interrupt();
-			} finally {
-				semaphore.release();
 			}
 		}
 	}

+ 0 - 1
hutool-core/src/test/java/cn/hutool/core/thread/ThreadUtilTest.java

@@ -10,6 +10,5 @@ public class ThreadUtilTest {
 		final boolean isValid = true;
 		
 		ThreadUtil.execute(() -> Assert.assertTrue(isValid));
-		
 	}
 }