D
David
Hi everybody,
I have a small application running a perpetual loop to listen events
from a database. I'd like to add a START / STOP control...
For example :
java MyApp -START should launch the process (infinite loop)
java myApp -STOP should stop the process
Thanks a lot in advance for an example... (Don't forget I'm a newbie)
I already coded a "addShutdownHook", but this is not really what I
want. Furthermore, I'm not able to properly interrupt my loop because
I never reach the /*never come here*/ section.... Maybe somebody
could have a look? I guess the problem is the ShutdownHook runs in a
separated thread...
class MyListener {
public static int i_interrupted = 0;
public static void main(String[] args) throws SQLException,
IOException {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("My listener received a kill");
i_interrupted = 1;
}
});
do {
try {
System.out.println("Sleeping 5 seconds");
Thread.sleep(5000);
... do a job here ...
} catch(InterruptedException e) {
System.out.println("Thread interrupted");
i_interrupted = 1;
}
} while (i_interrupted == 0);
/*never come here*/
}
I have a small application running a perpetual loop to listen events
from a database. I'd like to add a START / STOP control...
For example :
java MyApp -START should launch the process (infinite loop)
java myApp -STOP should stop the process
Thanks a lot in advance for an example... (Don't forget I'm a newbie)
I already coded a "addShutdownHook", but this is not really what I
want. Furthermore, I'm not able to properly interrupt my loop because
I never reach the /*never come here*/ section.... Maybe somebody
could have a look? I guess the problem is the ShutdownHook runs in a
separated thread...
class MyListener {
public static int i_interrupted = 0;
public static void main(String[] args) throws SQLException,
IOException {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println("My listener received a kill");
i_interrupted = 1;
}
});
do {
try {
System.out.println("Sleeping 5 seconds");
Thread.sleep(5000);
... do a job here ...
} catch(InterruptedException e) {
System.out.println("Thread interrupted");
i_interrupted = 1;
}
} while (i_interrupted == 0);
/*never come here*/
}