Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- volatile boolean stop = false;
- Runtime.getRuntime().addShutdownHook(new Thread("shutdown thread") {
- public void run()
- {
- stop = true;
- }
- });
- boolean stop = false;
- final Object event = new Object();
- Runtime.getRuntime().addShutdownHook(new Thread("shutdown thread") {
- public void run()
- {
- synchronized(event)
- {
- stop = true;
- event.notifyAll();
- }
- }
- });
- // ... and in your polling loop ...
- synchronized(event)
- {
- while(!stop)
- {
- // ... do JDBC access ...
- try
- {
- // wait 30 seconds, but break out as soon as the event is fired.
- event.wait(30000);
- }
- catch(InterruptedException e)
- {
- // Log a message and exit. Never ignore interrupted exception.
- break;
- }
- }
- }
- public class Service implements Runnable {
- private boolean shouldStop = false;
- public synchronized stop() {
- shouldStop = true;
- notify();
- }
- private synchronized shouldStop() {
- return shouldStop;
- }
- public void run() {
- setUp();
- while (!shouldStop()) {
- doStuff();
- sleep(60 * 1000);
- }
- }
- private synchronized sleep(long delay) {
- try {
- wait(delay);
- } catch (InterruptedException ie1) {
- /* ignore. */
- }
- }
- }
Add Comment
Please, Sign In to add comment