|
In the following sections, we discuss miscellaneous properties of threads: thread priorities, daemon threads, thread groups, and handlers for uncaught exceptions. Thread PrioritiesIn the Java programming language, every thread has a priority. By default, a thread inherits the priority of its parent thread, that is, the thread that started it. You can increase or decrease the priority of any thread with the setPriority method. You can set the priority to any value between MIN_PRIORITY (defined as 1 in the THRead class) and MAX_PRIORITY (defined as 10). NORM_PRIORITY is defined as 5. Whenever the thread-scheduler has a chance to pick a new thread, it prefers threads with higher priority. However, thread priorities are highly system dependent. When the virtual machine relies on the thread implementation of the host platform, the Java thread priorities are mapped to the priority levels of the host platform, which may have more or fewer thread priority levels. For example, Windows NT/XP has seven priority levels. Some of the Java priorities will map to the same operating system level. In the Sun JVM for Linux, thread priorities are ignored altogetherall threads have the same priority. Thus, it is best to treat thread priorities only as hints to the scheduler. You should never structure your programs so that their correct functioning depends on priority levels. CAUTION
java.lang.Thread 1.0
Daemon ThreadsYou can turn a thread into a daemon thread by calling t.setDaemon(true); There is nothing demonic about such a thread. A daemon is simply a thread that has no other role in life than to serve others. Examples are timer threads that send regular "timer ticks" to other threads. When only daemon threads remain, the virtual machine exits. There is no point in keeping the program running if all remaining threads are daemons. java.lang.Thread 1.0
Thread GroupsSome programs contain quite a few threads. It then becomes useful to categorize them by functionality. For example, consider an Internet browser. If many threads are trying to acquire images from a server and the user clicks on a Stop button to interrupt the loading of the current page, then it is handy to have a way of interrupting all these threads simultaneously. The Java programming language lets you construct what it calls a thread group so that you can simultaneously work with a group of threads. You construct a thread group with the constructor: String groupName = . . .; ThreadGroup g = new ThreadGroup(groupName) The string argument of the ThreadGroup constructor identifies the group and must be unique. You then add threads to the thread group by specifying the thread group in the thread constructor. Thread t = new Thread(g, threadName); To find out whether any threads of a particular group are still runnable, use the activeCount method. if (g.activeCount() == 0) { // all threads in the group g have stopped } To interrupt all threads in a thread group, simply call interrupt on the group object. g.interrupt(); // interrupt all threads in group g However, executors let you achieve the same task without requiring the use of thread groupssee page 63. Thread groups can have child subgroups. By default, a newly created thread group becomes a child of the current thread group. But you can also explicitly name the parent group in the constructor (see the API notes). Methods such as activeCount and interrupt refer to all threads in their group and all child groups. java.lang.Thread 1.0
java.lang.ThreadGroup 1.0
Handlers for Uncaught ExceptionsThe run method of a thread cannot throw any checked exceptions, but it can be terminated by an unchecked exception. In that case, the thread dies. However, there is no catch clause to which the exception can be propagated. Instead, just before the thread dies, the exception is passed to a handler for uncaught exceptions. The handler must belong to a class that implements the THRead.UncaughtExceptionHandler interface. That interface has a single method, void uncaughtException(Thread t, Throwable e) As of JDK 5.0, you can install a handler into any thread with the setUncaughtExceptionHandler method. You can also install a default handler for all threads with the static method setDefaultUncaughtExceptionHandler of the Thread class. A replacement handler might use the logging API to send reports of uncaught exceptions into a log file. If you don't install a default handler, the default handler is null. However, if you don't install a handler for an individual thread, the handler is the thread's THReadGroup object. The ThreadGroup class implements the THRead.UncaughtExceptionHandler interface. Its uncaughtException method takes the following action:
That is the stack trace that you have undoubtedly seen many times in your programs. NOTE
java.lang.Thread 1.0
java.lang.Thread.UncaughtExceptionHandler 5.0
java.lang.ThreadGroup 1.0
|
|