Thread.sleep is called while a monitor is held
What it finds
Thread.sleep inside synchronized code keeps the monitor for the whole pause: every other thread that needs it waits too, for nothing. lock.wait(timeout) gives the monitor up while waiting, and sleeping outside the block holds nothing.
From the rule's source, sleep_while_locked.rs.
class Poller { private final Object lock = new Object(); void poll() throws InterruptedException { synchronized (lock) { Thread.sleep(100);tx0 reports: the thread sleeps while it holds a monitor } Thread.sleep(100); } synchronized void retry() throws InterruptedException { Thread.sleep(10);tx0 reports: the thread sleeps while it holds a monitor Runnable r = () -> { try { Thread.sleep(10); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:sleep-while-locked" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:sleep-while-locked] reason="..."tx0 explain tx0/java:sleep-while-locked shows the same in the terminal. Found a false positive? Report it.