Home Java Thread Multithreading in Java


Share on Google+Share on Google+

Multithreading in Java

This tutorial explains you Multithreading in Java.

Multithreading in Java



So far you have learned about a single thread. Lets us know about the concept of multithreading and learn the implementation of it. But before that, lets be aware from the multitasking.

Multitasking :

Multitasking allow to execute more than one tasks at the same time, a task being a program. In multitasking only one CPU is involved but it can switches from one program to another program so quickly that's why it gives the appearance of executing all of the programs at the same time. Multitasking allow processes (i.e. programs) to run concurrently on the program. For Example running the spreadsheet program and you are working with word processor also.  
Multitasking is running heavyweight processes by a single OS.

Multithreading :

Multithreading is a technique that allows a program or a process to execute many tasks concurrently (at the same time and parallel). It allows a process to run its tasks in parallel mode on a single processor system 

In the multithreading concept, several multiple lightweight processes are run in a single process/task or program by a single processor. For Example, When you use a word processor you perform a many different tasks such as printing, spell checking and so on. Multithreaded software treats each process as a separate program.


In Java, the Java Virtual Machine (JVM) allows an application to have multiple threads of execution running concurrently. It allows a program to be more responsible to the user. When a program contains multiple threads then the CPU can switch between the two threads to execute them at the same time. 
For example, look at the diagram shown as:

In this diagram, two threads are being executed having more than one task. The task of each thread is switched to the task of another thread.

Advantages of multithreading over multitasking :  

  • Reduces the computation time.
  • Improves performance of an application.
  • Threads share the same address space so it saves the memory.
  • Context switching between threads is usually less expensive than between processes. 
  • Cost of communication between threads is relatively low.

Different states implementing Multiple-Threads are:

As we have seen different states that may be occur with the single thread. A running thread can enter to any non-runnable state, depending on the circumstances. A thread cannot enters directly to the running state from non-runnable state, firstly it goes to runnable state. Now lets understand the some non-runnable states which may be occur handling the multithreads.
  • Sleeping ? On this state, the thread is still alive but it is not runnable, it might be return to runnable state later, if a particular event occurs. On this state a thread sleeps for a specified amount of time. You can use the method sleep( ) to stop the running state of a thread.
       static void sleep(long millisecond) throws InterruptedException
  • Waiting for Notification ? A thread waits for notification from another thread. The thread sends back to runnable state after sending notification from another thread.
       final void wait(long timeout) throws InterruptedException
       final void wait(long timeout, int nanos) throws InterruptedException
       final void wait() throws InterruptedException

  • Blocked on I/O ? The thread waits for completion of blocking operation. A thread can enter on this state because of waiting I/O resource. In that case the thread sends back to runnable state after availability of resources.
  • Blocked for joint completion ? The thread can come on this state because of waiting the completion of another thread.
  • Blocked for lock acquisition ? The thread can come on this state because of waiting to acquire the lock of an object.

If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 3, 2006

Related Tutorials

Ask Questions?    Discuss: Multithreading in Java   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
venkata rao
April 18, 2011

in multithreading their are two threads. 1.foreground 2. background in this case which thread wille be executed frist?
venkata rao
April 18, 2011

1. how to execute main(-) method in java ? 2.in method oveloading concept returntype is doesnot consider why ? int task(String str); float task(String rry);
vivek kaushik
May 25, 2011

it can be better if you try some more
June 8, 2011

it is nice .great experience
irfan ullah
July 7, 2011
main thread of java

sir, why we keep main thread static and public
June 21, 2011

interviewer ask Multithreading concept deply so how prepare this concept deaply tell me plz
August 18, 2011

want to know
August 21, 2011

all the java progaram i want
August 22, 2011

this defination and diagram is very good,and one more thing if you are given the with example its its good,ok its not a mater what ever it is very good.
alemu tiku
October 18, 2011

can CPU process more than one task
November 22, 2011
give me tutorial

please text me any material on topic generic,collection applet &multithreading
November 25, 2011

nice way to understand
mir inayat ullah
November 30, 2011

any simple programe for this topic plzsend me.thanx
December 6, 2011
about above topic

that is great job ..well done
NareshKumar. S
December 21, 2011

Little bit help us.. But I am not completely Understand What u are Saying...
December 24, 2011

It's Vrey Useful One
January 10, 2012
what is meant by context switching?

your explanation is simple.
January 10, 2012
API in java

can you give full explanation regarding the topic
January 23, 2012

like computer science
January 23, 2012

The tutorial is useful
January 23, 2012

awsm written in points but more explanation is required ...
jeya kumar.j
January 28, 2012

send the multithreading java programs
papendra katre
March 19, 2012
good job

iam happy java
Er.Rakesh Pandya
April 16, 2012

Nice Concept Declared
April 29, 2012

gud but can do better
June 10, 2012

November 16, 2012

Which are the important question in the point of university exams i need a reply before the 22th of novembar ........Please send the reply soon
June 24, 2013

why used multithreading in java programming?