Multithreading in java

                             Multithreading in java


Those who are familiar with the modern operating systems such as Windows 95 and Windows XP may recognize that they can execute several programs simultaneously.This ability is known as multitasking. In systems terminology, it is called multithreading. Multithreading is a conceptual programming paradigm where a program is divided into or more subprograms, which can be implemented at the same time in parallel. For example, one subprogram can display an animation on the screen while another way builds the next animation to the displayed. Multithreading means dividing a program into multiple small-small parts.

Multithreading is used to achieve multitasking.
A thread is a process. Execution of more than one thread in a single instance.

Advantages of Java Multithreading

1) you can perform multiple operations at the same time because threads are independents.

2)You can save time by using multithreading.


Multitasking is a process of executing multiple tasks at same time. We use multitasking to utilize the CPU. Multitasking can be achieved in two ways:

  • Multithreading
  • Multiprocessing
Thread-based Multitasking (Multithreading)
  •     Threads share the same address space.
  •     Cost of communication between the thread is low.
  •     A thread is lightweight.
Process-based Multitasking (Multiprocessing)
  •        Each process allocates separate memory area.
  •       A process is heavyweight.
  •      Cost of communication between the process is high.

The Life cycle of Thread:-

There are many states of thread life cycle.

        1.       Newborn State

        2.       Runnable State

        3.       Running State

        4.       Blocked State

        5.       Dead State

   ·    Newborn State:-

When we create a thread object, the thread is born and this state is called newborn state. The thread is not yet scheduled for running. this state is gone to the next state by using start() method.

1.         Schedule it for running using start() method.
2.         Kill it using stop() method.

If scheduled, it moves to the runnable state. If we use any other method at this stage, an exception will be thrown.
    Runnable State:-      

It means the thread is ready for execution. When two process has same priority than it is solve this problem by using round robin fashion. Which is said that first come first serve.


When a thread is starting to executing, it states is changed to Running. The scheduler picks one of the thread from the runnable state and changes this state to Running. Then CPU starts executing this thread. A thread can easily change state to Dead, Runnable and Blocked from running state depends on time slicing. 
   Blocked State:-    
A blocked state is state where the process is stop. And the process is going to the waiting state.
   Dead State:-     

Every thread has a life cycle.It is a natural death. The meaning of dead state is the process is totally destroys at the time. And it is not come back in running state.

 Commonly used methods of Thread class:

  •     public void run(): It is used to perform an action for a thread.
  •     public void yield(): It is used to go from running state to runnable state.  

  •     public Thread currentThread(): returns/pass the reference of currently executing thread.

  •   public int setPriority: changes the priority of the thread.

  •  public void resume(): resume() method is used to suspend a thread or process.
  •    public void stop(): stop() method is use for stop a thread or process.
  •    public String getName(): returns the thread name. 
  •   public int getPriority(): returns the thread priority.
  •    public void setName(String name): changes the thread name.