System.nanoTime — measuring elapsed time
System.nanoTime() returns a long reading in nanoseconds. Subtract readings to measure elapsed time, not a date.
CKTIS / Java
Programming syntax, operators and concepts. Find an explanation, explore an example and return to learning.
System.nanoTime() returns a long reading in nanoseconds. Subtract readings to measure elapsed time, not a date.
Apply calendar rules through LocalDate.with(...). Distinguish next from nextOrSame and find the last day of a month with lastDayOfMonth, without modifying the input date.
The `Thread(Runnable)` constructor creates a platform thread and associates it with a task without starting it. Use `start()` to launch the thread and `join()` to wait for it to finish.
Get the Thread object for the code that is running now, and learn how to restore its interrupt status after catching InterruptedException.
Thread.getName() returns the specified thread's name as a String. The method takes no arguments and does not start the thread.
An interrupt is a signal, not a forced stop. Learn when `InterruptedException` means the interrupt status was cleared and how to restore it.
Use `Thread.join` to wait for a thread to terminate. Learn which overloads confirm termination and when the waiting thread can safely read the worker’s result.
One shared `ThreadLocal` instance keeps a separate value for each thread. Removing a value lets the next read initialize it again, but the mechanism does not copy objects.
Create platform threads from Runnable tasks, start them once, read their results after joining, and handle cooperative cancellation without losing interrupt status.
Thread.onSpinWait() tells the runtime that a loop is busy-waiting. It may enable an optimization, but it neither replaces synchronization nor guarantees a speedup.
ThreadPoolExecutor manages a worker pool and a task queue. Learn when it creates threads or queues work, how ThreadPoolExecutor.CallerRunsPolicy handles overload, and how shutdown() and awaitTermination() support orderly termination.
Give worker threads names such as `worker1`, `worker2`, and `worker3` so you can tell them apart in logs and while debugging.
`Thread.sleep(long)` pauses the current thread for a requested number of milliseconds. It does not wait for another thread to finish or guarantee task order.
`Thread.start()` schedules code to run in a separate thread. Calling `run()` directly does not start a thread; use `join()` when you need to wait for it to finish.
`getCause()` returns an exception’s immediate cause or `null`. Call it on an `ExecutionException` to inspect the exception thrown by the task.
`getMessage()` returns an exception’s detail message, or `null` if there is no message. Calling the method does not print anything.
Use `getSuppressed()` to read secondary exceptions recorded on a primary exception, such as when both a `try` block and automatic resource cleanup fail.
Use `TimeUnit.MILLISECONDS` to give `tryLock` a timeout in milliseconds. Each call gets its own timeout and can acquire the lock immediately.
Convert milliseconds to nanoseconds and pass the matching unit when setting a timeout for `tryLock`.
`TreeMap()` creates an empty map that sorts keys by their natural order. Use `TreeMap::new` as the map supplier when `groupingBy` should return sorted group keys.
In Java SE 21, get a handle to a plain `int` field, choose memory ordering for reads and writes, and conditionally update the value in one atomic step.
Learn how five WeakHashMap operations behave in Java SE 21 when keys are weak, values are strong, and null can mean more than one thing.
WeakReference<T> holds a weak reference that does not prevent the garbage collector from reclaiming an object. Reading it through get() may return null; clear() clears the reference without destroying the object itself.
Create a zoned date-time, convert it without changing the instant, format it for display, and edit local fields while accounting for time-zone transitions.