Java Thread destroy


 

Java Thread destroy

In this tutorial, we are using Thread.destroy() method to destroy the thread.

In this tutorial, we are using Thread.destroy() method to destroy the thread.

Java Thread destroy

In this tutorial, we are using Thread.destroy() method to destroy the thread.

Thread destroy() :

Thread class provides destroy method to destroy the thread. In general, Thread.destroy() is dangerous and not implemented. The basic idea is to kill the thread and break its monitor locks. But if it is done at the middle of any process then the deadlock condition occur. So usually we don't use this method.

public void destroy() : This method destroy your thread without any cleanup. The locked monitor remains lock. Implementation of this method is not done.

Example :

class ThreadDestroy implements Runnable{
	public static void main(String args[]){
	    Thread thread=new Thread();
	    thread.start();
	    try {
	      //destroy  thread without any cleanup.
	      thread.destroy();
	    } catch (Throwable e) {
	      System.out.println("destroy() throws "+e+"("+e.getMessage()+")");
	    }
	  }

	@Override
	public void run() {
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
				e.printStackTrace();
		}
		
	}
}

Output :

destroy() throws java.lang.NoSuchMethodError(null)

Ads