|
The upstairs is pretty good, let me explain in detail:
1. The stop () method is used to terminate all unfinished methods, including the run () method. When a thread is stopped, all locks on the object it released are released. For example: Suppose you design a thread to transfer money from A's account to B. To ensure security, a lock will be used. Suppose you call stop () when the money is withdrawn from A's account and not transferred to B's account The method stops the thread, at which point the state of the object will be inconsistent. Therefore, we do not know when it is safe to call the stop () method, so it is not recommended.
2. The suspend () method is used to block the thread and change the state of the thread to Blocked. The thread will only be restarted when another thread calls the resume () method. However, all threads calling the suspend () method will suspend all, resulting in a deadlock. So the suspend () method is not recommended, and of course resume () is useless!
The correct method is interrupt (), send an interrupt request to a thread, set the interrupt status to true, and throw an InterruptedException when the thread status is blocked. To check if a thread is terminated, use the isinterrupted () method. |
|