Threads  in Java ..

Posted on Updated on

Threads  in Java

Java is a multi-threaded programming language which means programmer  can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run simultaneously (concurrently) and each part can execute a different code  at the same time making maximum use of the available resources. Resources mean available CPU time or memory or both.

Process vs. Thread 

To understand concurrency and multi threading, one must first understand differences between a process and a thread.

process runs independently and isolated of other processes. It cannot directly access shared data in other processes. The resources of the process, e.g. memory and CPU time, are allocated to it through  the operating system.

thread is a so called lightweight process. It has its own call stack (order in which various methods are called), but can access shared data of other threads in the same process. Every thread has its own memory cache. Many threads run within a single process. This reduces consumption of resources.  If a thread reads shared data it stores this data in its own memory cache.

By definition, multitasking is when multiple processes sharing  common processing resources such as a CPU time. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads  run in parallel. Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program.

The idea of a thread is that of a lightweight unit of execution—smaller than a process, but still capable of executing arbitrary Java code. The usual way that this is implemented is for each thread to be a fully fledged unit of execution to the operating system but to belong to a process, with the address space of the process being shared between all threads comprising that process. This means that each thread can be scheduled independently and has its own stack and program counter but shares memory and objects with other threads in the same process.

Life Cycle Of a Thread

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread.

Following are the stages of the life cycle −

  • New− A new thread begins its life cycle in the new state. The thread has been created (its constructor has been called).It remains in this state until the program starts the thread.
  • Runnable– The thread’s start() method has been called,the thread becomes Runnable. The thread is available to be run(execute code) by the thread scheduler.
  • Waiting– The thread has been temporarily removed from the RunnableA thread transitions back to the Runnable state only when another thread signals the waiting thread to continue executing.
  • Timed Waiting(also known as Blocked)− A runnable thread can enter the timed waiting state for a specified interval of time. This is achieved by calling sleep() method. This method can take argument in terms of time interval. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs. An event like completion of I/O
  • Terminated (Dead)− A runnable thread enters the terminated state when it finishes execution of run() method.  It remains there until the application ends.


Diagram below depicts various state of a thread and shows method  which will transform a thread from one state to another state. Here Ready state is also called as Runnable state .

   The diagram represents all the phases of life cycle of a thread. 


Figure 1. Life cycle representing all phases of a thread.

Multithreading environment in java can be implemented in two ways as the diagram below shows.

  1. Create a class(user defined) which extends java built-in Thread class.
  2. Create a class (user defined) which implements java built-in Runnable interface.

Classes and interfaces to create threads


Thread (Class): A class that creates and defines thread. This class inherits the Object class and implements Runnable interface.

Runnable(interface): The only method in this interface is the run() method.

Object: wait(), notify() and  notifyAll() are methods of this class used for threading.

Example of single thread running by creating a class which extends Thread class.


class SinglethreadDemo extends Thread

{                                   // method of Runnable interface

public void run() {

System.out.println (” My single thread is in running state”); }


public static void main (String args[]) {

SinglethreadDemo obj = new SinglethreadDemo ();

obj.start();                             // Calling start() method of Thread class




For further information call : Vissicomp Technology    26708190/9320957718


Written and Edited By                                                                 Vissicomp Technology

Dr. Ashwin I Mehta

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s