Home Java Thread Java Multithreading Example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java Multithreading Example

Advertisement
In Java Multithreading we can create multiple threads to run different tasks. This example will demonstrate you step-by-step how to run different tasks using different threads.

Java Multithreading Example

In this section we will discuss how we can do a different task using the different threads.

In Java Multithreading we can create multiple threads to run different tasks. This example will demonstrate you step-by-step how to run different tasks using different threads.

Example

Here I am giving a simple example which will demonstrate you about how to run multiple tasks using multiple threads. In this example we will do three different tasks using three different threads. Each thread will be responsible for its own task only. Among these three threads one will find the average number of the input numbers, one will be responsible for finding the Maximum number from the input array of numbers, and one will be responsible for finding the Minimum number from the input array of numbers. In this example I have created three different classes which implements the Runnable interface to make a Thread class.

Source Code

AverageNumber.java

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

MaximumNumber.java

// File Name : 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

// File Name : 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);
}
}

ThreadClassDemo.java

// File Name : 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 :

When you will compile and execute the above example you will get the output as follows :

Download Source Code

Advertisement

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: March 2, 2013

Related Tutorials

Ask Questions?    Discuss: Java Multithreading Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Tom Hite
July 3, 2013
Thanks for the simple example -trying it out

Thanks for the simple example. I'm trying this out on 1.7.0_07-b11. And I am using Netbeans 7.2.1 to compile and run. I have run it inside Netbeans and from the Windows XP command prompt. It does run as suggested. However, most of the time it doesn't run completely. Generally it will print average, but the Max and particularly the Min may or may not show up. Typically I have to run this 5 or 6 times before this program will show all three results. If I substitute .run() for .start() in the main program then it always runs. But that is a cheat as I don't think .run() creates a separate thread.
DMCA.com