Java Timer hang issue -


i've been scratching head trying figure out hang issue java timers. wondering if here out. in diagnosing issue highly appreciated.

i have simple program 3 timertask classes (a, b, , stopper). , b run repeatedly every 400ms , 500ms respectively. stopper task scheduled run @ 2sec shutdown everything. timers fire expected, , run() methods of tasks execute expected. however, once stopper task executes, expect program terminate, hangs after printing "all tasks , timers canceled, exiting". i've tried using jstack diagnose problem there nothing obvious indicates what, if needs released/stopped/canceled etc.

here code:

package com.example.experiments;  import java.util.date;  /**   * test timer class check behavior of exit/hang issues  */ public class timertest {      timertest(){     }      class taska extends java.util.timertask {          taska(){         }         public void run() {             system.err.println("a.run() called.");              if (!running){                 system.err.println("a: calling this.cancel().");                 this.cancel();                 return;             }          }         public boolean cancel(){             system.err.println("canceling taska");             return super.cancel();         }     }      class taskb extends java.util.timertask {          taskb(){         }          public void run(){             system.err.println("b.run() called.");              if (!running){                 system.err.println("b: calling this.cancel().");                 this.cancel();                 return;             }          }         public boolean cancel(){             system.err.println("canceling taskb");             return super.cancel();         }     }       private void start(){         this.running = true; // flag indicate if server loop should continue running or not          final java.util.timer timera = new java.util.timer();         final taska taska = new taska();         timera.schedule(taska, 0, 400);          final java.util.timer timerb = new java.util.timer();         final taskb taskb = new taskb();         timerb.schedule(taskb, 0, 500);          class stoppertask extends java.util.timertask {             private java.util.timer mytimer;              stoppertask(java.util.timer timer){                 mytimer = timer;             }              public void run(){                 taska.cancel();                 taskb.cancel();                 timera.cancel();                 timerb.cancel();                  this.cancel();                 mytimer.cancel();                 system.err.println("stopper task completed");             }         }         final java.util.timer stoppertimer = new java.util.timer();         final stoppertask stoppertask = new stoppertask(stoppertimer);         stoppertimer.schedule(stoppertask, 2*1000);           /** register witjh jvm notified on when jvm exit */         java.lang.runtime.getruntime().addshutdownhook(new thread() {             @override             public void run() {                 system.err.println("shutting down...");                 running = false;                  taska.cancel();                 taskb.cancel();                 timera.cancel();                 timerb.cancel();                  stoppertask.cancel();                 stoppertimer.cancel();                  system.err.println("all tasks , timers canceled, exiting");                 system.exit(0);             }         });           }      public static void main(string[] args) {         new timertest().start();     }      private boolean running = false; } 

instead of system.exit(0) perform return. also, should marking running variable volatile.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -