This question is again going to check your in depth knowledge of thread methods i.e. destroy() method is deadlock prone. If the target thread holds a lock on object when it is destroyed, no thread can lock this object (Deadlock formed are similar to deadlock formed when suspend() and resume() methods are used improperly). It results in deadlock formation. These deadlocks are generally called Frozen processes.
Additionally you must know calling destroy() method on Threads throw runtimeException i.e. NoSuchMethodError.
Destroy() method puts thread from running to dead state.
Program to see usage of destroy() method (usage has been shown for illustration of method, but we must not use this deadlock prone deprecated method) >
public class DestroyTest {
public static void main(String[] args) throws InterruptedException {
final Thread thread1=new Thread("Thread-1"){
public void run() {
System.out.println(Thread.currentThread().getName()+" has started.");
Thread.currentThread().destroy();
System.out.println(Thread.currentThread().getName()+" has ENDED.");
}
};
thread1.start();
}
}
/*
Thread-1 has started.Exception in thread "Thread-1"
java.lang.NoSuchMethodError
at java.lang.Thread.destroy(Unknown Source)
at DestroyTest$1.run(DestroyTest.java:8)
*/
|
If we note output of the program calling destroy() method throwed runtimeException NoSuchMethodError. And thread ended immediately without printing " has ENDED.".
RELATED LINKS>
run() and Start() methods >
What will happen if we don’t override run method of thread?
What will happen if we override start method of thread?
Difference between starting thread with run() and start() methods in java
Can we start Thread again?
Important Thread methods (salient features, usage with programs)>
Join() method - ensure all threads that started from main must end in order in which they started and also main should end in last. Types of join() method-10 salient features of join
Sleep() method in threads - 10 key features with programs
Yield() method in threads - 8 key features with programs
Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.
Daemon threads - 12 salient features of Daemon Thread
2 alternate ways to stop thread, as stop() method is deprecated
Using Suspend and resume method in threads
Object and class lock >