Saturday, January 23, 2010

Thread-related Exceptions in Java

This post summarizes in one place the thread-related exceptions in Java.



1) IllegalMonitorStateException is thrown when
  • the wait, notify, or notifyAll methods are called by a thread that has not locked the associated object.
2) IllegalThreadStateException is thrown when
  • the start method is called and the thread has already been started.
  • the setDaemon method has been called and the thread has already been started.
  • an attempt is made to destroy a nonempty thread group.
  • an attempt is made to place a thread into a thread group (via the Thread constructor) and the thread group has already been destroyed.
  • an attempt is made to get the exit value of a nonterminated Process.
3) SecurityException is thrown by the security manager when
  • the Thread constructor has been called and it is requested that the thread to be created be placed into a thread group for which it has no security permission.
  • a stop or destroy method has been called on a thread for which the caller does not have the correct permission for the operation requested.
  • the ThreadGroup constructor has been called with a parent group parameter for which the calling group has no permission.
  • a stop or destroy method has been called on a thread group for which the caller does not have the correct permission for the operation requested.
4) NullPointerException is thrown when
  • a null pointer is passed to the stop method.
  • a null pointer is passed to the ThreadGroup constructor for the parent group.
5) InterruptedException is thrown when
  • a thread that has made a join method call is woken up by the thread being interrupted rather than by the target thread terminating.
  • a thread that has made a wait method call is woken up by the thread being interrupted rather than by a notify or notifyAll.
  • a thread that has made a waitFor method call is woken up by the thread being interrupted rather than by the target process terminating.

No comments:

Post a Comment