How does thread’s stop method work

How does thread’s stop method work?

Stop() method of thread stops the thread execution.

The security manager, if present, checks if the current thread is trying to stop some other thread. The stop method to stop the thread is an unsafe option. Stopping a thread causes it to unlock all the monitors it has locked. It causes the damaged objects to be read by some other threads. These objects are damaged because they are left in an inconsistent state on stopping the thread.

It is always better to write a code which will modify some variable to indicate that the target thread should stop running. The target thread should check this variable regularly, and return from its run method in an orderly fashion if the variable indicates that it is to stop running.

How does thread’s stop method work?

The threads stop method has been deprecated. It causes a sudden termination of a Thread's run method. If the tasks performed by the run method are critical, then the stop method could leave the program in an inconsistent state.

The stop method throws a ThreadDeath object at the thread to kill it due to which the thread dies asynchronously.
How do we specify pause times in my program?
How do we specify pause times in my program? - Using the sleep function in Java, the thread’s execution can be put on hold. During this pause session....
Multithreaded program and importance of thread synchronization
Multithreaded program and importance of thread synchronization - A multithreaded program involves multiple threads of control in a single program. Each thread has its own stack...
When a thread is created and started, what is its initial state?
When a thread is created and started, what is its initial state? - A thread is in “Ready” state after it has been created and started...
Post your comment