Why Thread.stop(), Thread.suspend(), and Thread.resume() Methods are Deprecated After JDK 1.1 Version?
The Thread class contains constructors and methods for creating and operating on threads. Thread is a subclass of Object that implements the Runnable interface. There are many methods in Thread Class but some of them are deprecated as of JDK 1.1. In this article, we will understand the reason behind it.
Deprecated methods are those that are no longer considered important and should not be utilized because they may be removed from their class. Classes evolve over time, leading their APIs to alter, resulting in deprecation. Attributes change, methods are renamed, and new ones are added. To help developers shift from the old API to the new one, deprecated classes and methods are marked @deprecated in documentation comments.
Why deprecated ??
Thread.stop() is being phased out due to its inherent risk. When you stop a thread, it unlocks all the monitors it has locked. Other threads might see these objects in an inconsistent state if any of the objects previously protected by these monitors were in an inconsistent state.
Threads acting on damaged objects can act erratically, whether consciously or unconsciously. ThreadDeath, unlike other uncontrolled exceptions, silently kills threads, leaving the user with no warning that the program may be corrupted. After the damage has occurred, the corruption may appear at an unpredicted moment. Also, killing a thread will create a problem while working with DBMS – JDBC in a multithreaded environment.
Thread.suspend() is deprecated because it is inherently deadlock-prone. As a result, Thread.resume() must be deprecated as well. When the target thread is suspended, it holds a lock on the monitor protecting a crucial system resource, and no other thread may access it until the target thread is resumed. Deadlock occurs if the thread that would restart the target thread tries to lock this monitor before invoking resume().
Example of possible deadlock from using these deprecated methods:
THREAD NAME : Thread1 Putting value 1 in the NumVal Array Main Thread 500 Main Thread 501 Thread1 is awake now THREAD NAME : Thread1 Putting value 2 in the NumVal Array Thread1 is awake now //Deadlock is created & hence no output after this
Explanation: We created 2 threads: thread1 & thread2. Both want to acquire a lock on the NumVal object referred to by ‘v’ reference.
As we start both the threads by calling the start() method, the run() method will execute whenever the thread gets CPU. Thread1 gets CPU & when the value of n is 2 in its run method, the thread is suspended. Thread1 is not going to release lock on Object referred by ‘v’ until resume() is called on it.
For thread2 to acquire a lock on v Object referred by ‘v’, thread1 must have released the lock on Object referred by ‘v’. Here the lock here is not released, thread2 will keep on waiting for thread1 to release the lock on the object referred by ‘v’ & a deadlock will be formed.
So, you have always call resume() on a thread (at any time) whenever you call suspend() method on the same thread.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.