multithreading - Java: Thread/task expiration after specified milliseconds -
in java there sane way have thread/task run continuously , end after specified run time (preferably without using several timers)?
for instance, if have timertask, there doesn't seem way schedule task end after number of milliseconds or @ specific time timer class.
sure, can schedule task repeat after number of milliseconds, if want end after 1 iteration? have run timer within scheduled task? i'm hoping more elegant answer that.
the answer provided in this question work, not had in mind.
essentially, i'm looking similar autoreset property on c#'s system.timers.timer
class
you can use executorservice
, grab future
, .cancel()
after time want:
final future<whatever> f = executor.submit(...); timeunit.seconds.sleep(xxx); f.cancel(true);
or can have 2 services: 1 executes, uses scheduledexecutorservice
cancellation.
note: timertask
depends on system time, use scheduledexecutorservice
instead.
Comments
Post a Comment