Thread Priorities in Java

Java assigns each thread a priority that determines how that thread should be treated for the others. Thread priorities are integers that specify the relative importance of one thread to another.

A priority is meaningless as an absolute value; a higher priority thread does not run any faster than a lower-priority thread if it is the only thread running. Instead, a thread’s priority is used to decide when to switch from one running thread to next. This is called a context switch. The rules that determine when a context switch takes place are simple:

  • A thread can voluntarily (on its own) relinquish control. This is done by explicitly yielding, sleeping or blocking or pending I/O. In this scenario, all other threads are examined, and normally the highest- priority thread that is ready to run is given the CPU.
  • A higher-priority thread can pre-empt a thread. In this case, a lower priority thread that does not yield the processor is pre-empted no matter what it is doing, by a higher priority thread. As soon as a higher-priority thread wants to run, it does. This is called preemptive multitasking.
  • Some OS support non-preemptive priority based scheduling. In such case a high priority thread gets chance only when low priority thread completes.
  • In cases where two threads with the same priority are competing for CPU cycles, the situation is a bit complicated. For OS such as windows98, equal priority threads must voluntarily (on their own) yield (give up) control to their peers. If they do not, the other threads will not run.
Note:-

Problems can arise from the differences in the way that O.S.’s context-switch threads of equal priority.

Synchronization:

Because multi-threading introduces as asynchronous behaviour to our programs, there must be a way for us to enforce synchronization when we need it. For example, if we want two threads to communicate and share a complicated data structure, such as a linked list, we need some way to ensure that they do not conflict with each other.

We must prevent one thread from writing data while another thread is in the middle of reading it. Java uses monitor for inter-thread synchronization. We can think of a monitor as a tiny box that can hold only one thread. Once a thread enters a monitor can be used to protect a shared asset from being manipulated by more than one thread at a time.

Most multi-threaded systems expose as objects that our program must explicitly acquire and lock. Java provides a cleaner solution.

There is no class “monitor”; instead, each object has its implicit monitor that is automatically entered when one of the object’s synchronized method is called. Once a thread is inside a synchronized method, no other thread can call any other synchronized method on the same object. This enables us to write very clear and concise multi-threaded code because synchronization support is built into the language.

Messaging:

When programming with most other languages, we must depend on the O.S. to establish communication between threads.

This, of course, adds overhead. By contrast, Java provides a clean, low-cost way for two or more threads to talk to each other, via calls to predefined methods that all objects have. Java’s messaging system allows a thread to enter synchronized method on an object, and then wait there until some other thread explicitly notifies to come out.

The Thread class and the Runnable interface:

Java’s multi-threading system is built upon the Thread class, its methods, and its companion interface, Runnable. This class belongs to package java.lang and hence there is no need of explicitly importing it.

Thread encapsulates a thread of execution since we cannot directly refer to a running thread’s internal state, we will deal with it through its proxy, the Thread instance that spawned it. To create a new thread, our program will either extend Thread class or implement the Runnable interface. The Thread class defines several methods that help manage threads:

String getName()

Returns this thread’s name

int getPriority()

Returns this thread’s priority

boolean isAlive()

Tests if this thread is still running

void join()

Waits for this thread to die (terminate)

void run()

If this thread was constructed using a separate Runnable object, then that Runnable object’s run method is called; otherwise, this method does nothing and returns, if thread class is extended and run() method is overridden in sub-class then the overridden run() method is called.

void setName(String name)

void setName(String name) Changes the name of this thread to be equal to the argument name

static void sleep(long millis) throws InterruptedException

static void sleep(long millis) throws InterruptedException Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.(1 sec = 1000ms)

static void sleep(long millis, int nanos) throws InterruptedException

static void sleep(long millis, int nanos) throws InterruptedException Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds.

void start()

void start() Causes this thread to begin execution. The Java Virtual Machine calls the run method of this thread.

static void yield()

static void yield() Causes the currently executing thread object to pause and allow other threads to execute temporarily.

static Thread currentThread()

static Thread currentThread() Returns a reference to the currently executing thread object.

The main thread:

When a Java program starts up, one thread begins running immediately. This is usually called the main thread of our program because it is the one that is executed when our program begins. The main thread is the thread from which other “child” threads are created.

Although the main thread is created automatically when our program is started, it can be controlled through a Thread object. To do so, we must reference it by calling the method currentThread(), which is a public static member of Thread class.

This method returns a reference to the thread in which it is called. Once we have reference to the main method, we can control it just like any other thread.

Copyright 1999- Ducat Creative, All rights reserved.