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.


 

Ads

Tutorials   
Threading In Java Example Thread Life Cycle Example in java What Is Thread In Java? Creating multiple Threads Daemon Threads Synchronization in java with example Thread priority in java Threads in Java Java Multithreading Example Thread Synchronization in Java Overview of Networking through JAVA,How to retrieve URL information URL in term of Java Network Programming Overview of Networking through JAVA,Convert URI to URL Socket and ports Server Sockets Overview of Networking through JAVA,Find the Host name in reverse of given IP address Print the URL of a URLConnection Networking in Java Computer Networking Overview of Networking through JAVA,Getting the Local port Overview of Networking through JAVA Overview of Networking through JAVA Overview of Networking through JAVA,Getting list of Local Interfaces on a machine Overview of Networking through JAVA Overview of Networking through JAVA,Find Your Host Name Overview of Networking through JAVA,To retrieve the IP address from Host Name, vice-versa Open a URLConnection to specific website address Return the MIME Header Construct a DatagramSocket on an unspecified port Datagram in network environment Construct a DatagramPacket to receive data Client-Server Architecture Thread Priorities Synchronized Threads Multithreading in Java Interthread Communication Creation of MultiThreads Threading in Java Thread Priorities Inter-Thread Communication Synchronized Threads Creation of Multiple Threads Threading in Java Daemon Threads Deadlock Synchronization Life Cycle of Threads Thread Constructors Thread Creation Overview of Thread
Ads

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

Advertisements

Share on Google+Share on Google+

Java Multithreading Example

Posted on: March 2, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Java Multithreading Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
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.

Ads

 

Ads