The RuntimeException is not thrown directly by any of the methods in the thread classes; it is simply a base class that specifies a special group of exceptions. Runtime exceptions are considered so basic that it would be too tedious to check for every possible runtime exception that could be thrown (another reason is that these exceptions are generally bugs in the program). Unlike other exceptions, the compiler does not require that you handle a RuntimeException.
All of the following exceptions are runtime exceptions.
The IllegalThreadStateException is thrown by the thread classes when the thread is not in a state where it is possible to fulfill the request. This is caused by an illegal request made by the program and generally indicates a bug in the program. The following are the possible cases in the thread system where the IllegalThreadStateException is thrown:
The Thread class provides the start()
method,
which starts a new thread (see Chapter 2). As we
mentioned, a thread should be started only once. However, if a
program calls the start()
method of an already
running thread, the IllegalThreadStateException is thrown.
The Thread class provides the setDaemon()
method, which specifies whether the thread is a daemon thread (see
Chapter 6). As we mentioned, the daemon status of
a thread must be set before the thread is started. If the
setDaemon()
method is called when the thread is
already running, the IllegalThreadStateException is thrown.
The Thread class provides the countStackFrames()
method, which determines how deep in the call stack the thread is
currently executing (see Appendix A). A thread must
be suspended in order for this count to take place. If the thread is
not suspended when this method is called, the
Illegal-ThreadStateException is thrown.
The ThreadGroup class provides the destroy()
method to allow the thread group to be destroyed (see Chapter 10). A ThreadGroup instance can only be destroyed
when the group does not contain any threads and does not contain any
groups that contain threads. If the destroy()
method is called on a group that contains threads or is already
destroyed, the IllegalThreadStateException is thrown.
The Thread class contains certain constructors that allow the thread to be placed into a specific thread group (see Chapter 10). The thread group that is passed to these constructors must not have been destroyed; if the constructor is passed a thread group that has been destroyed, the IllegalThreadStateException is thrown.
It is possible to call methods of the thread classes with incorrect parameters. When this is done, an IllegalArgumentException is thrown. Only one method related to the Thread classes throws the exception:
The Thread class provides the setPriority()
method, which controls the priority assigned to the thread (see Chapter 6). The priority that is assigned must fall
between the system minimum and maximum priorities. If the priority
requested is not within this range, an IllegalArgumentException is
thrown. (The setPriority()
method may also throw
a security exception; see the section Section 2.4.5,” later in this appendix.)
The IllegalThreadStateException is actually a subclass of the IllegalArgument-Exception class; if you attempt to catch objects of type IllegalArgumentException, you will also catch objects of type IllegalThreadStateException.
The IllegalMonitorStateException is thrown by the Thread system when an operation on a wait monitor is attempted and the state of the monitor is not valid for the operation to take place. Currently, the only operation that involves this exception is the wait and notify mechanism; grabbing or releasing the lock itself is not a method call and hence cannot throw an exception.
The Object class provides the wait()
method,
which allows a thread to wait for a notification condition (see Chapter 4). The wait()
method must
be called while the synchronization lock for the object is held. The
wait()
method is also overloaded with two other
method signatures that allow the program to specify a timeout. If any
of these methods is called without owning the synchronization lock,
the IllegalMonitorStateException is thrown.
The Object class provides the notify()
method,
which allows a thread to send a notification signal to any threads
waiting (see Chapter 4). The
notify()
method must be called while the
synchronization lock for the object is held. The Object class also
provides the notifyAll()
method, which wakes up
all the waiting threads. If either of these methods is called without
owning the synchronization lock, the IllegalMonitorStateException is
thrown.
The thread classes throw this exception in the following cases:
The Thread class provides a version of the
stop()
method that allows the user to specify
the object used to stop the thread (see Appendix A). Normally, programs do not use this method;
however, if a program does use this method and passes a null object
to stop a thread, the NullPointerException is thrown.
The ThreadGroup class provides a version of its constructor that allows the application to specify the parent group (see Chapter 10). If null is specified for the parent group, the NullPointerException is thrown.
In addition, the NullPointerException can be thrown by the Java virtual machine itself while it is executing code within the thread classes.
Most methods of the Thread and ThreadGroup classes can throw a Security- Exception. The SecurityException can be thrown by the following methods:
The Thread class provides the checkAccess()
method, which simply calls the security manager to determine if the
thread can be accessed by the current thread group (see Chapter 10). A SecurityException is thrown if access is
not permitted. For a complete list of methods that call the
checkAccess()
method, see Figure 10.1.
The ThreadGroup class provides the checkAccess()
method, which simply calls the security manager to determine if the
thread group can be accessed by the current thread group (see Chapter 10). A SecurityException is thrown if access is
not permitted. For a complete list of methods that call the
checkAccess()
method, see Figure 10.1.
The Thread class provides the setPriority()
method, which sets the scheduling priority of the thread. The
priority requested must be less than the maximum priority of the
thread group to which the thread belongs. If the priority is greater
than this maximum priority, a SecurityException may be thrown (see
Chapter 10).
The stop()
method of the Thread class may throw
a security exception under Java 2 and later releases if the
stopThread
permission has not been granted to
the code that is calling the stop()
method (see
Chapter 10).
Arbitrary runtime exceptions may be thrown by the following method:
The run()
method of the Thread class executes
user-specific code and, hence, can throw any runtime exception the
user code does not catch. Exceptions that the
run()
method throws are caught in the manner we
describe in Appendix A.