multithreading - Java, how to make a thread that watches the state of a program? -
multithreading - Java, how to make a thread that watches the state of a program? -
i want create thread running in background checking state of programme runs. want know how maintain running , how create thread.
executors.newsinglethreadexecutor().submit(new applicationmonitor()); class applicationmonitor implements runnable { public void run() { // monitoring stuff } }
applicationmonitor should never homecoming , never allow exceptions thrown. alternately, , maybe more safely, create applicationmonitor 1 check, , set submit() phone call in loop. monitoring can fail, , restarted later:
while (true) { seek { future<?> future = executors.newsinglethreadexecutor().submit( new applicationmonitor()); future.get(); // can add together timeout here limit monitoring thread } grab (exception e) { reportmonitoringexception(e); } sleepuntilnextmonitoringcycle(); }
finally, can have java scheduling you:
scheduledexecutorservice executorservice = executors.newscheduledthreadpool(1); executorservice.schedulewithfixeddelay( new applicationmonitor(), 0, 30, timeunit.minutes);
with approach, can't future scheduled invocations, exceptions have handled within applicationmonitor.
java multithreading state
Comments
Post a Comment