multithreading - What causes scheduled threads not to run in Java? -
i have developed small java application runs 2 scheduled threads via scheduled executor service. on computers application runs fine. in testing have come across computer threads not running should or not @ all. have 1 thread scheduled run on 250 ms intervals. checks see if there read on std in, if there reads , executes command. thread runs sporadically never should. other thread runs every 5 seconds , prints screen. runs once , never gets ran again. here code using:
scheduledthreadmanager.schedulewithfixeddelay(new runnable() { @override public void run() { try { if(inputreader.ready()) { string command = inputreader.readline(); executecommand(command); } } catch(ioexception e) { system.out.println(e.tostring()); e.printstacktrace(); } } }, 250, 250, timeunit.milliseconds); scheduledthreadmanager.schedulewithfixeddelay(new runnable() { @override public void run() { system.out.println(idlestring); } }, 0, 5000, timeunit.milliseconds);
i have made sure app not hanging during execution of scheduled threads. computer has core2duo processor in can't see how hardware not able meet needs, perhaps not so. other interesting thing running main application thread in addition these , running correctly. input cause of problem appreciated.
you said " have 1 thread scheduled run on 250 ms intervals", that's not you've programmed do. used schedulewithfixeddelay
, , means "leave 250ms between end of 1 execution, , start of next". if task takes 100ms execute, it'll 350ms between executions, , if task's execution time varies lot, intervals @ gets executed.
creates , executes periodic action becomes enabled first after given initial delay, , subsequently given delay between termination of 1 execution , commencement of next.
have @ scheduleatfixedrate
instead, has "run every x millis" semantics, , may more need:
creates , executes periodic action becomes enabled first after given initial delay, , subsequently given period; executions commence after initialdelay initialdelay+period, initialdelay + 2 * period, , on.
the big danger scheduleatfixedrate
tasks can end overlapping, if configure wrong, or tasks take long execute.
Comments
Post a Comment