Home Java Beginners Multithreading Example In Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Multithreading Example In Java

Advertisement
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 AverageNumber.java, MaximumNumber.java, MinimumNumber.java represents the three different threads and the ThreadClassDemo.java is the main class to execute the application.

MaximumNumber.java

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);
}
}

MinimumNumber.java

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);
}
}

AverageNumber.java

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);
}
}

ThreadClassDemo.java

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);
thread1.setDaemon(true);
thread1.setName("AvgNum");
System.out.println(thread1.getName() + " is starting...");
thread1.start();

Runnable bye = new MaximumNumber(numarr);
Thread thread2 = new Thread(bye);
thread2.setPriority(Thread.MIN_PRIORITY);
thread2.setDaemon(true);
thread2.setName("MaxNum");
System.out.println(thread2.getName() + " is starting...");
thread2.start();

Runnable min = new MinimumNumber(numarr);
Thread thread3 = new Thread(min);
thread3.setPriority(Thread.MIN_PRIORITY);
thread3.setDaemon(true);
thread3.setName("MinNum");
System.out.println(thread3.getName() + " is starting...");
thread3.start(); 
}
}

Output

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

Download Source Code

Advertisements

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

Ask Questions?    Discuss: Multithreading Example In Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com