Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Java Beginners Multithreading Example In Java


Share on Google+Share on Google+

Multithreading Example In Java

This section describes you how to do work with multiple threads in Java.

Multithreading Example In Java

In this section we will learn about multithreading in Java.

Multithreading in Java is used to execute multiple tasks at the same time or parallel. Support of Multithreading in Java allows to process multiple tasks in parallel on a single processor. JVM allows for such type of execution i.e. a Java application can contain the concurrently executable multiple threads. In the multithreading environment CPU can switch between the two threads to execute them concurrently.

In multithreading in Java some of the non-runnable states which are used to handle multithreading are as follows :

  • Sleeping : sleep() method is used to stop the running state of thread.
  • Waiting For Notification : wait() method is used to send the thread into waiting state.
  • Blocked On I/O : This is a state into which the thread is reached because of waiting I/O resource.
  • Bolocked For joint completion : This is a state into which the thread is reached because of waiting for the process completion of another thread.
  • Blocked For locked acquisition : This is a state into which the thread is reached because of waiting for acquiring of lock of an object.

Example :

Here I am giving a simple example of Multithreading. In this example you will see that we have created the four different class. Among these classes,, represents the three different threads and the is the main class to execute the application.

public class MaximumNumber implements Runnable
private int[] number;
public MaximumNumber(int[] number)
this.number = number;
public void run()
int largest = number[0];
for(int i=1; i< number.length; i++)
if(number[i] > largest)
largest = number[i]; 
System.out.println("Largest Number is : " + largest);

public class MinimumNumber implements Runnable
private int[] nmb;
public MinimumNumber(int[] nmb)
this.nmb = nmb;
public void run()
int smallest = nmb[0];
for(int i=1; i< nmb.length-1; i++)
if (nmb[i] < smallest)
smallest = nmb[i]; 
System.out.println("Smallest Number is : " + smallest);

public class AverageNumber implements Runnable
int n;
private int[] num;
double avg;
public AverageNumber(int[] num)
this.num = num;
public void run()

for(int i = 1; i < num.length-1; i++)
n = n+num[i];
avg = (double)n/i;
System.out.println("Average Number is : "+avg);

public class ThreadClassDemo

public static void main(String [] args)
int[] numarr = new int[6];
for(int i=0; i<5; i++)
numarr[i] = Integer.parseInt(args[i]);
Runnable hello = new AverageNumber(numarr);
Thread thread1 = new Thread(hello);
System.out.println(thread1.getName() + " is starting...");

Runnable bye = new MaximumNumber(numarr);
Thread thread2 = new Thread(bye);
System.out.println(thread2.getName() + " is starting...");

Runnable min = new MinimumNumber(numarr);
Thread thread3 = new Thread(min);
System.out.println(thread3.getName() + " is starting...");


This application takes the input at command line. So after compiling successfully the give the input at the command line while executing the application as follows :

Download Source Code


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: August 1, 2013

Related Tutorials

Discuss: Multithreading Example In Java  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image