Deprecated Thread Methods (stop, suspend, resume)
ID |
java.thread_deprecated_methods |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
CWE:477, concurrency, reliability |
Description
Reports calls to Thread.stop(), Thread.suspend(), and Thread.resume(), which have been deprecated since JDK 1.2 due to inherent safety issues. These methods are unsafe for concurrent programming and should never be used in modern code.
Rationale
Thread.stop() forces a thread to terminate by throwing a ThreadDeath error. This can leave shared data structures in an inconsistent state because monitors are released abruptly without completing their critical sections.
Thread.suspend() and Thread.resume() are deadlock-prone: if a suspended thread holds a lock, the thread calling resume() may need that same lock, creating an unresolvable deadlock.
// Dangerous -- abrupt termination corrupts shared state
Thread worker = new Thread(task);
worker.start();
worker.stop(); // never do this
// Dangerous -- deadlock-prone
worker.suspend(); // never do this
worker.resume(); // never do this
Remediation
Use cooperative cancellation via Thread.interrupt() and check Thread.isInterrupted() or catch InterruptedException in the worker:
// Safe -- cooperative cancellation
Thread worker = new Thread(() -> {
while (!Thread.currentThread().isInterrupted()) {
doWork();
}
});
worker.start();
worker.interrupt(); // request graceful shutdown