Java Multithreading Example

Ads
 

Java Multithreading Example

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.

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

Ads

Ads