Share on Google+Share on Google+

Java Thread destroy

Advertisement
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)

Advertisements

Advertisement

Posted on: November 8, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.