Thread Properties Core Java

In the following sections, we discuss miscellaneous properties of threads: thread priorities, daemon threads, thread groups, and handlers for uncaught exceptions.

Thread Priorities

In the Java programming language, every thread has a priority. By default, a thread inherits the priority of the thread that constructed 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).

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 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 altogether—all threads have the same priority.

Beginning programmers sometimes overuse thread priorities. There are few reasons ever to tweak priorites. You should certainly never structure your programs so at their correct functioning depends on priority levels

CAUTION: If you do use priorities, you should be aware of a common beginner’s error. If you have several threads with a high priority that don’t become inactive, the lower-priority threads may never execute. Whenever the scheduler decides to run a new thread, it will choose among the highest-priority threads first, even though that may starve the lowerpriority threads completely.

java.lang.Thread

  • void setPriority(int newPriority)
    sets the priority of this thread. The priority must be between Thread.MIN_PRIORITY and Thread.MAX_PRIORITY. Use Thread.NORM_PRIORITY for normal priority.
  • static int MIN_PRIORITY
    is the minimum priority that a Thread can have. The minimum priority value is 1.
  • static int NORM_PRIORITY
    is the default priority of a Thread. The default priority is 5.
  • static int MAX_PRIORITY
    is the maximum priority that a Thread can have. The maximum priority value is 10.
  • static void yield()
    causes the currently executing thread to yield. If there are other runnable threads with a priority at least as high as the priority of this thread, they will be scheduled next. Note that this is a static method.

Daemon Threads

You can turn a thread into a daemon thread by calling

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 or threads that clean up stale cache entries. When only daemon threads remain, the virtual machine exits. There is no point in keeping the program running if all remaining threads are daemons.

Daemon threads are sometimes mistakenly used by beginners who don’t want to think about shutdown actions. However, this can be dangerous. A daemon thread should never access a persistent resource such as a file or database since it can terminate at any time, even in the middle of an operation.

java.lang.Thread

  • void setDaemon(boolean isDaemon)
    marks this thread as a daemon thread or a user thread. This method must be called before the thread is started.

Handlers for Uncaught Exceptions

The 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 .Uncaught Exception Handler interface. That interface has a single method, void uncaught Exception (Thread t, Throwable e)

As of Java SE 5.0, you can install a handler into any thread with the set Uncaught Exception- Handler method. You can also install a default handler for all threads with the static method set Default Uncaught Exception Handler 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 Thread Group object.

NOTE: A thread group is a collection of threads that can be managed together. By default, all threads that you create belong to the same thread group, but it is possible to establishother groupings. Since Java SE 5.0 introduced better features for operating on collections ofthreads, you should not use thread groups in your own programs.

The ThreadGroup class implements the Thread.UncaughtExceptionHandler interface. Its uncaught- Exception method takes the following action:

  1. If the thread group has a parent, then the uncaughtException method of the parent group is called.
  2. Otherwise, if the Thread.getDefaultExceptionHandler method returns a non-null handler, it is called.
  3. Otherwise, if the Throwable is an instance of ThreadDeath, nothing happens.
  4. Otherwise, the name of the thread and the stack trace of the Throwable are printed on System.err. That is the stack trace that you have undoubtedly seen many times in your programs.

java.lang.Thread

  • static void setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
  • static Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler()
  • sets or gets the default handler for uncaught exceptions.

  • void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler)
  • Thread.UncaughtExceptionHandler getUncaughtExceptionHandler()

sets or gets the handler for uncaught exceptions. If no handler is installed, the thread group object is the handler.

java.lang.Thread

  • void uncaughtException(Thread t, Throwable e)
    defined to log a custom report when a thread is terminated with an uncaught exception.
  • void uncaughtException(Thread t, Throwable e)
    calls this method of the parent thread group if there is a parent, or calls the default handler of the Thread class if there is a default handler, or otherwise prints a stack trace to the standard error stream. (However, if e is a ThreadDeath object, the stack trace is suppressed. ThreadDeath objects are generated by the deprecated stopmethod.)

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics