mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
修复ThreadUtil
中中断异常处理丢失中断信息的问题,解决ConcurrencyTester资源未释放的问题(pr#1358@Gitee)
This commit is contained in:
@@ -16,8 +16,10 @@
|
|||||||
|
|
||||||
package cn.hutool.v7.core.thread;
|
package cn.hutool.v7.core.thread;
|
||||||
|
|
||||||
|
import cn.hutool.v7.core.io.IORuntimeException;
|
||||||
import cn.hutool.v7.core.util.RuntimeUtil;
|
import cn.hutool.v7.core.util.RuntimeUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.Thread.UncaughtExceptionHandler;
|
import java.lang.Thread.UncaughtExceptionHandler;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
@@ -443,6 +445,8 @@ public class ThreadUtil {
|
|||||||
try {
|
try {
|
||||||
timeUnit.sleep(timeout.longValue());
|
timeUnit.sleep(timeout.longValue());
|
||||||
} catch (final InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
|
// pr#1358 重新标记线程为中断状态(恢复中断信息),让后续代码能感知到“线程曾被中断过”
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -487,6 +491,8 @@ public class ThreadUtil {
|
|||||||
try {
|
try {
|
||||||
Thread.sleep(millis);
|
Thread.sleep(millis);
|
||||||
} catch (final InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
|
// pr#1358 重新标记线程为中断状态(恢复中断信息),让后续代码能感知到“线程曾被中断过”
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -708,8 +714,7 @@ public class ThreadUtil {
|
|||||||
* @since 3.1.2
|
* @since 3.1.2
|
||||||
*/
|
*/
|
||||||
public static ThreadGroup currentThreadGroup() {
|
public static ThreadGroup currentThreadGroup() {
|
||||||
final SecurityManager s = System.getSecurityManager();
|
return Thread.currentThread().getThreadGroup();
|
||||||
return (null != s) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -789,9 +794,12 @@ public class ThreadUtil {
|
|||||||
* @return {@link ConcurrencyTester}
|
* @return {@link ConcurrencyTester}
|
||||||
* @since 4.5.8
|
* @since 4.5.8
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("resource")
|
|
||||||
public static ConcurrencyTester concurrencyTest(final int threadSize, final Runnable runnable) {
|
public static ConcurrencyTester concurrencyTest(final int threadSize, final Runnable runnable) {
|
||||||
return (new ConcurrencyTester(threadSize)).test(runnable);
|
try (ConcurrencyTester tester = new ConcurrencyTester(threadSize)) {
|
||||||
|
return tester.test(runnable);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IORuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user