com.codingapi.tx.framework.thread.HookRunnable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tx-client Show documentation
Show all versions of tx-client Show documentation
tx-client project for Spring Boot
package com.codingapi.tx.framework.thread;
import com.codingapi.tx.Constants;
import java.util.concurrent.TimeUnit;
/**
* create by lorne on 2017/8/9
*/
public abstract class HookRunnable implements Runnable {
private volatile boolean hasOver;
@Override
public void run() {
Thread thread = new Thread() {
@Override
public void run() {
Constants.hasExit = true;
while (!hasOver) {
try {
TimeUnit.MILLISECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
if (!Constants.hasExit) {
Runtime.getRuntime().addShutdownHook(thread);
} else {
System.out.println("jvm has exit..");
return;
}
try {
run0();
} finally {
hasOver = true;
if (!thread.isAlive()) {
Runtime.getRuntime().removeShutdownHook(thread);
}
}
}
public abstract void run0();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy