Java Multithreading Introduction


A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process). Multithreaded programs contain two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously. For example, one thread is writing content on a file at the same time another thread is performing spelling check.

In Java, the word thread means two different things.

  • An instance of Thread class.
  • or, A thread of execution.

An instance of Thread class is just an object, like any other object in java. But a thread of execution means an individual "lightweight" process that has its own call stack. In java each thread has its own call stack.


The main thread

Even if you don't create any thread in your program, a thread called main thread is still created. Although the main thread is automatically created, you can control it by obtaining a reference to it by calling currentThread() method.

Two important things to know about main thread are,

  • It is the thread from which other threads will be produced.
  • main thread must be always the last thread to finish execution.
class MainThread
{
 public static void main(String[] args)
 {
  Thread f=Thread.currentThread();
  t.setName("MainThread");
  System.out.println("Name of thread is "+f);
 }
}
Output
 Name of thread is Thread[MainThread,5,main]

Life Cycle of a Thread

New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. 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.

Terminated: A runnable thread enters the terminated state when it completes its task or otherwise terminates.


Thread Priorities

Every thread has a priority that helps the operating system determine the order in which threads are scheduled for execution. In java thread priority ranges between,

  • MIN-PRIORITY (a constant of 1)
  • MAX-PRIORITY (a constant of 10)

By default every thread is given a NORM-PRIORITY(5). The main thread always have NORM-PRIORITY.


Advantage of Java Multithreading

It doesn't block the user because threads are independent and you can perform multiple operations at same time.

You can perform many operations together so it saves time.

Threads are independent so it doesn't affect other threads if exception occur in a single thread.


Multitasking

Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved by two ways:

  • Process-based Multitasking(Multiprocessing)
  • Thread-based Multitasking(Multithreading)

Process-based Multitasking (Multiprocessing)

Each process have its own address in memory i.e. each process allocates separate memory area.

Process is heavyweight.

Cost of communication between the process is high.

Switching from one process to another require some time for saving and loading registers, memory maps, updating lists etc.


Thread-based Multitasking (Multithreading)

Threads share the same address space.

Thread is lightweight.

Cost of communication between the thread is low.


Share this article on