Understanding Threads

  • What is a process?
  • What is a thread?
  • How multi-threaded system is useful?
  • Life cycle of a thread in Java
  • Characteristics of a thread
  • Overview of the main thread

Understanding the Concepts

Processes and Multi-Processing System

A process is a program under execution. Most of the modern day operating systems are multi-processing systems which manages multiple processes simultaneously. Operating system allocates certain resources like memory exclusively for each of the processes and gives CPU time to each of the processes in turns. If a process can not continue for the want of, let say an external input, OS gives the time slot for another process in waiting, if any.

Threads and Multi-Threaded System

Thread is an independent context of execution with in a process. A process can have more than one thread and all threads share the resources of the process. When one thread of a process can not continue further, other thread of the same process may be given the chance to use the time slot. Hence, the process can efficiently use the resources allocated to it.

An operating systems that supports this behavior is a multi-threaded operating system and programming language that gives constructs/API to create and manage threads is called Multi-threaded programming language. Java supports multi-threading.

Life Cycle of Thread

In java, a thread life cycle includes the states: new, runnable, waiting, timed-waiting, blocked and terminated..

  • New: Thread is just created.
  • Runnable: Thread is allocated with the required resources and waiting for the CPU.
  • Waiting: Thread is waiting for notifications or joins without timeout.
  • Timed-Waiting: Thread is waiting for notifications or joins without timeout.
  • Blocked: Waiting for acquiring lock.
  • Terminated: execution complete.

Thread characteristics

A priority is associated with each thread which ranges from 1 to 10. Default priority is 5. Thread with higher priority gets the access to resources first. If the OS employs preemptive scheduling, higher priority threads preempts lower priority threads.

Every thread belongs to a named thread group. Threads in a given group can be managed collectively.

A thread always starts as part of its owning process. Threads that execute even after the owning process exits are called daemon threads. Usually batch jobs run as daemon threads when there is no other non-daemon thread competing for the resources.

Every thread have an ID issued by the system. These Ids are unique among the threads in the system. ID can be reused.

Every thread have a name which needs not be unique.

Main Thread

A Thread object of Java represents a thread of execution. Java programs always have at least one thread. Its priority is 5. Current Thread can be accessed using static method currentThread.

Applying the Concepts

Objective

To print various characteristics of the currently running main thread.

Listing: MainThread.java

Explanation

At line 5, we are getting hold of the current thread, i.e. the main thread. And there after we probing it to find the details of the thread.

Results

Running the above listing results in the following typical output.

Going Beyond the Concepts

Interesting Questions

How many threads start when running a simple Java program which is not multi-threaded?

Running a Java program always results in running a JVM. JVM itself is a multi-threaded application. The program it is running is one of them. The other threads run garbage collectors and etc., Therefore, running even a single-threaded program results in creating more than one thread, always.

As threads are less expensive, can we create as many number of threads as we like?

Though they are less expensive, its only in comparison with the processes. They still cost system resources. So, its better to have a limit on the number of threads. Better idea is to use a pool of threads with a max limit.

Coding Exercises

  1. Change the name of the main thread to 'FirstThread'.
  2. Change the priority of the thread to 3. See what's the imptact.
  3. Make the current thread as daemon thread. Does it make sense?

Quiz

  1. When a non-daemon thread waiting for CPU, can a daemon thread gets the CPU under any circumstances?
  2. How is a thread group useful from the programming point of view?
  3. Can a daemon thread have priority of 9?
  4. Can thread names be duplicate?
  5. Is Garbage Collector of Java a daemon?
Krishna Mohan Koyya
2011-03-10