📄 threadcontrolhelper.java
字号:
package lib.commons.util;
public final class ThreadControlHelper {
private ThreadControlHelper() {
}
public static Thread startThread(Runnable threadRunnable) {
return startThread(threadRunnable, true);
}
public static Thread startThread(Runnable threadRunnable, boolean isDaemon) {
Thread thread = new Thread(threadRunnable);
thread.setDaemon(isDaemon);
thread.start();
return thread;
}
public static void stopThreadAsync(ThreadRunnable threadRunnable) {
stopThread(threadRunnable, 0);
}
public static void stopThreadSync(ThreadRunnable threadRunnable) {
stopThread(threadRunnable, 100);
}
public static void stopThread(ThreadRunnable threadRunnable,
long intervalWaitMillSeconds) {
if (null != threadRunnable && threadRunnable.isRunning()) {
threadRunnable.stop();
if (intervalWaitMillSeconds > 0) {
while (threadRunnable.isRunning()) {
try {
Thread.sleep(intervalWaitMillSeconds);
} catch (Exception ex) {
}
}
}
}
}
public static void threadWaitMillSeconds(ThreadRunnable threadRunnable,
long totalMillSeconds, long minSpanMillSeconds) {
if (null != threadRunnable && threadRunnable.getRunFlag()
&& minSpanMillSeconds > 0 && totalMillSeconds > 0) {
long sleepTimes = totalMillSeconds / minSpanMillSeconds;
long modMillSeconds = totalMillSeconds % minSpanMillSeconds;
sleepTimes += (modMillSeconds > 0 ? 1 : 0);
for (long i = 1; i < sleepTimes && threadRunnable.getRunFlag(); i++) {
try {
Thread.sleep(minSpanMillSeconds);
} catch (Exception ex) {
}
}
if (modMillSeconds > 0 && threadRunnable.getRunFlag()) {
try {
Thread.sleep(modMillSeconds);
} catch (Exception ex) {
}
}
} else if ((null == threadRunnable && totalMillSeconds > 0)||minSpanMillSeconds<1) {
try {
Thread.sleep(totalMillSeconds);
} catch (Exception ex) {
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -