java - Conditional scheduling of task -


i need schedule task using java timer, following:

  • send request every 5 minutes (which receives status response)

  • if (status != end state), query status every 10 seconds next 2 minutes until reaches end state. if doesn't reach end state end of 2 minutes, sleep 3 minutes , resend request.

  • after 3 failed attempts (checking status 2 mins 10s interval), if end state not reached, revert 5 minute check interval

logic sending request , querying status has been taken care of. couldn't figure out scheduling tasks @ various intervals.

i know run() method should remember last state of task can re-schedule task appropriately.

i'm new timers. appreciated.

don't use timer use scheduledexecutorservice. more flexible.

first create instance many threads might need - running tasks concurrently:

final scheduledexecutorservice service = executors.newscheduledthreadpool(numthreads); 

now, schedule task run @ specified interval, use scheduleatfixedrate method schedule task. keep pointer returned future:

final future<?> handle = service.scheduleatfixedrate(() -> {}, 0, 5, timeunit.minutes); 

where () -> {} java 8 lambda empty runnable. can pass want in here.

now, want run hour. schedule task runs in hour , cancels previous 1 using handle:

final future<?> cancellationhandle = service.schedule(() -> handle.cancel(false), 1, timeunit.hours); 

again () -> handle.cancel(false) java 8 lambda, time it's runnable calls handle.cancel().

to honest, however, looks busy waiting model. java's wait()/notify() methods threads wait other processes complete , woken instant happens.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -