我有一个Java应用程序,该应用程序具有一些无限运行的线程来轮询后端数据库。当JVM关闭时(通过weblogic UI),我需要为每个线程执行一些清理操作,例如连接到数据库并将正在运行的事务的状态从“进行中”设置为“未处理”。
是否有一种在每个线程中编写方法的方法,当JVM将要停止该线程时会调用该方法吗?
我了解关机钩子,但是我需要线程中变量的数据来了解Db中要更改的状态,并且在关机钩子的实现中,似乎启动了一个单独的钩子线程,该线程不会包含来自原始线程。
答案 0 :(得分:1)
由于您有权访问这些线程(我是说您可以修改其代码),因此您可以:
JFrame
,则添加一个windowClosing()
钩子以优雅地停止正在运行的Thread
,同时将框架的默认关闭操作设置为 DO_NOTHING_ON_CLOSE 。在下面的答案中查看我的代码... Thread
退出循环。Thread
将进行任何必要的更改(可以完全访问其所有变量),然后退出run()
方法。Thread
(即标记所有其他Thread
停止的Thread
)中,您将join()
每一个 finite < / em> Thread
。Thread
完全执行(即退出其run()
方法)。Thread
将呼叫例如 System.exit(0);
退出整个应用程序,因为这可能是用户最后想要的操作。 / li>
注意:这假设用户将通过常规操作关闭应用程序。例如,如果用户要通过Windows的“任务管理器”杀死该应用程序,则此方法将行不通。我不了解Thread
上的关机钩子...
遵循示例代码(阅读注释):
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GraceDown {
private static class MyThread extends Thread {
private boolean keepGoing = true; //This is the stopping flag.
private final int i; //In this case i marks the 'id' of the Thread, but it is also an arbitary variable that the Thread has acccess in its final moments...
public MyThread(final int i) {
this.i = i;
}
private synchronized boolean isKeepGoing() {
return keepGoing; //Tells if we should exit the loop and start the stopping operation...
}
public synchronized void startShutdown() {
keepGoing = false; //Tells we should exit the loop and start the stopping operation.
}
@Override
public void run() {
while (isKeepGoing()) { //After every complete loop, we check the if we can go on.
//Your 'infinite' loop actions go in here:
try { Thread.sleep(1000); } catch (final InterruptedException ie) {}
System.out.println("Thread " + i + " running...");
}
//Your gracefull shutdown actions go here...
System.out.println("Thread " + i + ": my stopping actions go here! Look, I have access to all my variables (such as i)! ;)");
}
}
public static void main(final String[] args) {
//Create and start the Threads:
final MyThread[] myThreads = new MyThread[5];
for (int i = 0; i < myThreads.length; ++i)
myThreads[i] = new MyThread(i);
for (int i = 0; i < myThreads.length; ++i)
myThreads[i].start();
final JFrame frame = new JFrame("Lets close me...");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); //IMPORTANT STEP! This means we are going to close the application instead of it being closed automatically when the user presses to close the window...
//Adding the 'hook':
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent wevt) {
System.out.println("Initializing shutdown...");
//This is where the thread is going to stop all others...
for (int i = 0; i < myThreads.length; ++i)
myThreads[i].startShutdown();
//Wait for all threads to stop:
for (int i = 0; i < myThreads.length; ++i)
try { myThreads[i].join(); } catch (final InterruptedException ie) { System.err.println("Error gracefully shutdowning the Thread!!!"); }
//Now we can exit the JVM:
System.out.println("Exiting JVM...");
System.exit(0); //Code '0' indicates exiting without error(s).
}
});
frame.getContentPane().add(new JLabel("Close this window and the Threads will shutdown gracefully...", JLabel.CENTER));
frame.pack();
frame.setLocationRelativeTo(null); //Put the packed frame on the center of the default screen.
frame.setVisible(true);
}
}
上面应该打印出这样的内容:
线程1正在运行...
线程4正在运行...
线程0正在运行...
线程2正在运行...
线程3正在运行...
线程3正在运行...
线程4正在运行...
线程2正在运行...
线程1正在运行...
线程0正在运行...
正在初始化关机...
线程2正在运行...
线程2:我的停止动作就在这里!看,我可以访问我的所有变量(例如i)! ;)
线程0正在运行...
线程0:我的停止动作在这里!看,我可以访问我的所有变量(例如i)! ;)
线程1正在运行...
线程4正在运行...
线程3正在运行...
主题3:我的停止动作在这里!看,我可以访问我的所有变量(例如i)! ;)
主题4:我的停止动作在这里!看,我可以访问我的所有变量(例如i)! ;)
线程1:我的停止动作就在这里!看,我可以访问我的所有变量(例如i)! ;)
退出JVM ...
这是一个最小的解决方案,我可以在您描述的问题中看到。