Hallo zusammen,
ich komme gerade wirklich nicht weiter und hoffe, jemand hier kann mir helfen.....
Ich habe folgenden einfachen Thread als Singleton implementiert:
Den versuche ich nun folgendermaßen zu stoppen:
So nun ist die log-Ausgabe von stopMonitoring immer die folgende:
Kann mir bitte jemand verraten was da schiefläuft?
Danke für alle Tips!
ich komme gerade wirklich nicht weiter und hoffe, jemand hier kann mir helfen.....
Ich habe folgenden einfachen Thread als Singleton implementiert:
Code:
public class MonitorRunner extends Thread {
private static Logger logger = Logger.getLogger(MonitorRunner.class);
private static MonitorRunner monRunner = null;
/**
* the "main"-method of the monitor, started by Thread.start()
*/
@Override
public void run() {
logger.info("starting monitoring....");
while(! isInterrupted()) {
/* 1.) activity cycle */
// for every directory
for(Iterator i = ConfConstants.SRC_DIRS.iterator(); i.hasNext();) {
// TODO: implement functionality
}
/* 2.) quiet cycle */
try {
logger.debug("putting thread to sleep....");
TimeUnit.SECONDS.sleep(ConfConstants.INT_CHECK_DIRS);
} catch (InterruptedException e) {
logger.debug("received interrupt signal while sleeping!");
logger.debug(e.getMessage());
logger.debug("stacktrace", e);
// internal interrupt flag gets reseted by the InterruptedException
// so we have to interrupt() again
interrupt();
}
}
}
public static MonitorRunner getMonRunner() {
if(monRunner == null)
monRunner = new MonitorRunner();
return monRunner;
}
private MonitorRunner () {}
}
Den versuche ich nun folgendermaßen zu stoppen:
Code:
private String stopMonitoring() {
String output = "";
output += "Attempted to stop monitoring:
";
logger.debug("Trying to stop monitoring thread....");
//check if thread is already stopped, alternative to isAlive():Thread.State tState = fileMon.getState();
if(!MonitorRunner.getMonRunner().isAlive()) {
output += "Monitoring had already been stopped!
";
logger.debug("Thread was already stopped");
return output;
}
// if it is alive, ask the thread to stop in a nice way...
logger.debug("trying to stop thread via interrupt()");
MonitorRunner.getMonRunner().interrupt();
Thread.State state = MonitorRunner.getMonRunner().getState();
logger.debug("Thread-state: " + state.toString());
// check if thread is still alive
if(MonitorRunner.getMonRunner().isAlive()) {
logger.info("could not stop thread via interrupt()!");
output += "could not stop thread via interrupt()!";
return output;
}
// if we came so far, thread was terminated normally
output += "Monitoring is stopped!";
logger.info("Monitoring thread was stopped!");
return output;
}
So nun ist die log-Ausgabe von stopMonitoring immer die folgende:
Code:
Trying to stop monitoring thread....
trying to stop thread via interrupt()
Thread-state: RUNNABLE
could not stop thread via interrupt()!
Kann mir bitte jemand verraten was da schiefläuft?
Danke für alle Tips!