O'Reilly logo

Java Threads, Second Edition by Henry Wong, Scott Oaks

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

RuntimeException

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.

IllegalThreadStateException

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 start() method

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 setDaemon() method

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required