Home Java Thread Threading In Java Example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Threading In Java Example

Advertisement
Threading in Java example explains you how to use Thread in Java.

Threading In Java Example

In this section we will read about how to create a thread and use it in Java.

There are the two mostly used methods for threading in Java. One is to create a subclass of Thread class and the another is to implement Runnable interface. Code for running as a thread must be written within that thread.

Using Runnable Object

Runnable is an interface that defines a method run() for executing the code in the thread. The object of a class, implements the Runnable interface (called Runnable object) is passed as an argument of the java.lang.Thread constructor. The run() method must be override while implementing the Runnable interface. Using this method you can provide its own  implementation. This method specifies the execution of code.

Example

package net.roseindia.threadinginjava;

public class PrintTableRunnable implements Runnable {
	
	int num;
	public PrintTableRunnable(int num)
	{
		this.num = num;
	}
	@Override
	public void run() {		
		
		//code for printing table of a number.
	}

}

To create and/or run a thread the following code should be written as follows :

package net.roseindia.threadinginjava;

public class RunnableMain {
	
	public static void main(String args[])
	{
		PrintTableRunnable ptr = new PrintTableRunnable(4);
		Thread ptrThread = new Thread(ptr);
		ptrThread.start();
	}
}

Using Subclass Thread

Thread is a class of java.lang package which also implements the Runnable interface. Therefore, the Thread class must overrides the run() method but, this method doesn't do anything that is why when extending a Thread class for threading, the run() method should be override to provide its own implementation. This method specifies the execution of code.

Example

package net.roseindia.threadinginjava;

public class PrintTableThread extends Thread {
	
	int num;
	public PrintTableThread(int num)
	{
		this.num = num;		
	}
	
	public void run()
	{
		//code for printing table of a number
	}
}

To create and/or run a thread following code should be written as follows :

package net.roseindia.threadinginjava;

public class ThreadMain {
	
	public static void main(String[] args) {

		PrintTableThread ptt = new PrintTableThread(4);
		ptt.start();
	}

}

There are several static methods defined by the Thread class for managing the thread. These methods are used for providing information about the thread, affect the status of thread etc. We will see some of the methods in the example given below.

Example

Here we are giving a simple example using which you can understand the concept of threading in Java. You will see the static methods used for various purposes. Here we will discuss both of the above ways of creating Thread i.e. using Runnable Object and Subclass of Thread class. However, both of the example will display a table for the given number upto 10 multiplication. In the Runnable object example we will create a class then we will implement the Runnable interface and its run() method. We will create a constructor for initializing a value of variable which table will be displayed. To display a table of a number we will write the table display logic inside the run() method. Then we will create a main method class where we will create the Runnable object and then we will move the Thread into running state using start() method. But, in the Subclass of Thread class method we will create a class by extending the Thread class. Then we will override the run() method to provide its own implementation for displaying table of a number. Then we will create a main method class where will create an instance of the Thread class and then we will move the Thread into running state using start() method.

Example using Runnable Object

PrintTableRunnable.java

public class PrintTableRunnable implements Runnable {

int num;
public PrintTableRunnable(int num)
{
this.num = num;
}
@Override
public void run() { 
System.out.println("Table of "+num+" upto 10 multiplication."); 
for(int i=1; i<= 10; i++)
{
System.out.println(num+" X "+i+"= "+num*i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Table Completed");
}
}

RunnableMain.java

public class RunnableMain {
	
	public static void main(String args[])
	{
		PrintTableRunnable ptr = new PrintTableRunnable(4);
		Thread ptrThread = new Thread(ptr);
		ptrThread.start();
	}
}

Example Using Subclass Thread

PrintTableThread.java

public class PrintTableThread extends Thread {

int num;
public PrintTableThread(int num)
{
this.num = num; 
}

public void run()
{
System.out.println("Table of "+num+" upto 10 multiplication."); 
for(int i=1; i<= 10; i++)
{
System.out.println(num+" X "+i+"= "+num*i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) { 
e.printStackTrace();
}
}
System.out.println("Table Completed");
}
}

ThreadMain.java

package net.roseindia.threadinginjava;

public class ThreadMain {
	
	public static void main(String[] args) {

		PrintTableThread ptt = new PrintTableThread(4);
		ptt.start();		
	}

}

Output

When you will execute the either of the both classes i.e. RunnableMain.java and ThreadMain.java 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: July 25, 2013

Related Tutorials

Ask Questions?    Discuss: Threading In Java Example  

Post your Comment


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