我正在使用 jsf 编写一个 java ee 应用程序。我定义了一些后台进程,例如定期更新数据库等。这是代码:
public class AppServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent arg0) {
zamanli zm = new zamanli();
try {
zm.programBasla();
} catch (MalformedURLException ex) {
Logger.getLogger(AppServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AppServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
和班级:
public class zamanli {
public void programBasla() throws MalformedURLException, IOException {
int delay = 5000; //5 sn sonra başlar
int period = 600000; //10 dkda tekrar
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
// p = r.exec("c:\\WINDOWS\\system32\\calc");
System.out.println(Now());
} catch (Exception e) {
System.out.println("Çalışmadı");
}
try {
getCurrentExchangeValue();
} catch (MalformedURLException ex) {
Logger.getLogger(zamanli.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(zamanli.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
问题是,程序完成后,即使我关闭了项目,所以我的数据库也在不断更新。那么当程序关闭时我怎样才能杀死线程呢?
谢谢