A constructor starts a thread before the object is fully built
What it finds
A thread started in a constructor can run before the constructor has finished: if it reaches the object under construction, through this or an inner class, it sees fields not yet set, and a subclass's constructor has not run at all. Starting the thread in a separate method, after construction, avoids that.
From the rule's source, early_thread_start.rs.
class Server { private final Thread worker = new Thread(() -> {}); Server() { worker.start();tx0 reports: a thread is started before the constructor has finished } void begin() { worker.start(); }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:early-thread-start" = "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:early-thread-start] reason="..."tx0 explain tx0/java:early-thread-start shows the same in the terminal. Found a false positive? Report it.