To make the current thread to park for a specified timeout,i can use:
LockSupport.parkNanos(long nanos);
or
Thread.sleep(long millis);//internally same as Object.wait(long) i guess.
Unfortunatly,the internal ticking of this two method are all base on the system-clock.
If the system-clock is changed(by ntptime or manually) during the waiting period,these method would not acting as excpected.
for example, in t0,i invoke this method like Thread.sleep(dt) to sleep dt seconds. During the waiting period, i set my system-clock ahead 20 seconds,then the thread will sleep for actully dt+20 seconds.
use the following code to check it:
public static void main(String[] args) throws InterruptedException {
long s = System.nanoTime();
int _10sec = 10 * 1000;
LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(_10sec));
//or Thread.sleep(_10sec);
long e = System.nanoTime();
long used = e - s;
System.out.println("Used:" + TimeUnit.NANOSECONDS.toMillis(used)); }
use something like "date && sudo date -s 16:10:40" to set your system clock ahead for few minites during waiting, and you will see what happend from the output.
That's sad,because in my product,many things are base on system clock,
such as Scheduler(invoking LockSupport.park finally) for a heartbeat checker via a Socket connection.
If the system clock was set forward longer than the timeout,the Scheduler will consider it timed out,but actually not.
Any one encountered the same trouble and how do you solve it?
EDIT:
In my HA product,i using Watchdog to kill the os when a heavy-load is blocking the system(so called suicide strategy). I have to feed the dog internally to prevent unwanted suicide,Using ThreadSupport.parkNanos(xxx) will have unexpected suicide