Implement the padlock

This commit is contained in:
Rui Hu
2024-03-12 15:18:46 +08:00
parent 4953d93d5e
commit 6c798c04bd
15 changed files with 700 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
package com.cleverthis.interview.padlock;
class Utils {
/**
* Ensure we will wait a given amount of time even if there are interruptions.
*
* @param millis The time you want to sleep, measure in millisecond.
*/
public static void ensureSleep(long millis) {
long endTime = System.currentTimeMillis() + millis;
while (endTime > System.currentTimeMillis()) {
try {
//noinspection BusyWait
Thread.sleep(endTime - System.currentTimeMillis());
} catch (InterruptedException e) {
// do nothing when interrupted, will re-sleep in next loop
} catch (IllegalArgumentException e) {
break;
}
}
}
}