java - how to check if a scheduled thread has finished the task? -
i have task executes every 20 seconds . problem task can make delay cause interruption.
package client; import java.util.concurrent.scheduledthreadpoolexecutor; import static java.util.concurrent.timeunit.*; import java.util.date; public class bootstrap { public static void main(string[] args) { addthreadstopool(); servicetest service1 = new servicetest(); } public static void addthreadstopool() { scheduledthreadpoolexecutor eventpool = new scheduledthreadpoolexecutor(5); eventpool.scheduleatfixedrate(new updater(), 2, 20, seconds); try { thread.sleep(20000); } catch (interruptedexception ignored) { } } }
in example , class updater executes every 20 seconds , problem if updater 's execution takes period of time more 20 seconds . how make updater finishes ites task before next scheduled execution?
there no other way code updater completes in less 20 seconds. in strict sense impossible because thread may scheduled off cpu, long gc pause can happen, etc. if run loop inside task, periodically check time , abort if hit deadline.
Comments
Post a Comment