java - Repeat something after a specific time interval -
i have been going through of questions doing after specific interval of time (like printing hello world every 5 seconds).
i saw different ways can in java program. question how java internally.
once run java program, main function starts executing in thread. thread can sent runnable state anytime(pause execution). if had stated print statement in main function, how java keep track of time now. if java program not resumed next 5 seconds?
one way work if meant "every 5 seconds in time period java program running" . how jvm this?
assume have single processor.
ok, lets trace calls. if using scheduledthreadpoolexecutor
can find uses delayedworkqueue
internally:
super(corepoolsize, integer.max_value, 0, timeunit.nanoseconds, new delayedworkqueue());
to await next tasks delayedworkqueue
uses condition available = lock.newcondition()
:
available.awaitnanos(delay);
ok, lets take @ awaitnanos
implementation in abstractqueuedsynchronizer
:
locksupport.parknanos(this, nanostimeout);
and locksupport
:
unsafe.park(false, nanos);
this native method uses operating systems's scheduler delay thread execution.
Comments
Post a Comment