From 7c4c6c7af169d01c7e6318e332608936e7d43682 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 26 May 2020 15:58:22 +0800 Subject: [PATCH] fix bug --- CHANGELOG.md | 1 + .../hutool/core/thread/SemaphoreRunnable.java | 22 ++++++++++++++----- .../cn/hutool/core/thread/ThreadUtilTest.java | 1 - 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fe9acd26..00ff2872a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ ### Bug修复 * 【core 】 修复SimpleCache死锁问题(issue#I1HOKB@Gitee) +* 【core 】 修复SemaphoreRunnable释放问题(issue#I1HLQQ@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/thread/SemaphoreRunnable.java b/hutool-core/src/main/java/cn/hutool/core/thread/SemaphoreRunnable.java index 22cac27b0..c81fd06da 100644 --- a/hutool-core/src/main/java/cn/hutool/core/thread/SemaphoreRunnable.java +++ b/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(); } } } diff --git a/hutool-core/src/test/java/cn/hutool/core/thread/ThreadUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/thread/ThreadUtilTest.java index b5483e2b0..94e898611 100644 --- a/hutool-core/src/test/java/cn/hutool/core/thread/ThreadUtilTest.java +++ b/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)); - } }