CKTIS / Java
TimeUnit: milliseconds, nanoseconds, and timed tryLock
Convert milliseconds to nanoseconds and pass the matching unit when setting a timeout for `tryLock`.
Version: Java SE 21
Syntax
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
long timeoutNanos = TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
// For either timed tryLock call, catch InterruptedException or declare it in the enclosing method's throws clause.
boolean acquired = lock.tryLock(timeoutMillis, TimeUnit.MILLISECONDS);
// Alternative to the preceding call, not a second acquisition:
// boolean acquired = lock.tryLock(timeoutNanos, TimeUnit.NANOSECONDS);TimeUnit.MILLISECONDS is a constant of the java.util.concurrent.TimeUnit enum. It names a unit (a millisecond), not a duration value or a clock reading. TimeUnit.toNanos is an instance method: unit.toNanos(duration) takes a long in unit and returns a long in nanoseconds.
One millisecond equals 1,000,000 nanoseconds, so TimeUnit.MILLISECONDS.toNanos(250L) returns 250000000L. If the exact result is outside the long range, the method returns Long.MAX_VALUE above the upper bound or Long.MIN_VALUE below the lower bound. It does not throw on overflow or wrap the result.
In ReentrantLock.tryLock(long, TimeUnit), the unit tells the method how to interpret the first argument. Pass lock.tryLock(250L, TimeUnit.MILLISECONDS) directly, or use lock.tryLock(250000000L, TimeUnit.NANOSECONDS) after conversion. Both specify the same wait limit for acquiring the lock. Do not pair the converted number with MILLISECONDS; using nanoseconds as the unit does not guarantee nanosecond timing precision.
The timed overload returns true on acquisition (including reentrant acquisition by the same thread) and false if the limit expires without acquisition. A timeout <= 0 means no waiting. This overload honors the lock's fairness setting and throws InterruptedException, clearing the interrupt status, if the thread is interrupted on entry or during acquisition. The no-argument tryLock() does not wait, can bypass queued threads, and does not throw InterruptedException. A null unit in the timed overload causes NullPointerException. Call unlock() in finally only after a successful acquisition.
Examples
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
public static void main(String[] args) throws InterruptedException {
long timeoutMillis = 250L;
long timeoutNanos = TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
System.out.println(timeoutMillis + " ms = " + timeoutNanos + " ns");
ReentrantLock lock = new ReentrantLock();
if (lock.tryLock(timeoutMillis, TimeUnit.MILLISECONDS)) {
try {
System.out.println("Lock acquired");
} finally {
lock.unlock();
}
} else {
System.out.println("Lock not acquired");
}
}
}Result
250 ms = 250000000 ns Lock acquired
The program prints the conversion but passes the original millisecond value to `tryLock`. The new lock is free and no other thread competes for it, so the example shows successful acquisition, not timeout expiry.
Related lessons
- Java · Lesson 54 · Intermediate · Version: 2