Java Runnable Thread Sample Code

Java Threads can be very useful when you need to multi-process different tasks. You have two options for Java threading.

The first option, and preferred, is to create a thread from implementing the Java Runnable Interface. Mainly you can implement the Runnable and later extend the class. Not something you can do with the extend Thread option.

The other option creates a thread this based on extending the Java Thread class. As said already, this option does not allow you to then extend the class.


Java Runnable Thread Example

This simple class implements the Java Runnable Interface that will print out the numbers 1 through 10 in the background while sleeping in between iterations.

//Very Simple Class That Implements Runnable
class JavaCodeDepotRunnable implements Runnable
{
	
	//Put your tasks inside the run() method
	public void run()
	{
		//Print blank line
		System.out.println("");
		
		//Loop for 1 through 10
		for(int x=1; x<=10; x++)
		{
			System.out.println("Runnable Working on Number: "+x);
			
			//Sleep for 10 seconds to show it running in background
			try
			{
				Thread.sleep(10000);
			}
			catch(Exception ex) 
			{
				System.out.println("Exception:"+ex.toString());
			}
		}
	}
}


Java Thread Example

This simple class extends the Java Thread Class that will print out the numbers 11 through 20 in the background while sleeping in between iterations

//Very Simple Class That Extending Thread Class
class JavaCodeDepotThread extends Thread
{
	//Empty Constructor
	public JavaCodeDepotThread()
	{
		super();
	}
	
	//Put your tasks inside the run() method
	public void run()
	{
		//Print blank line
		System.out.println("");
		
		//Loop for 111 through 20
		for(int x=11; x<=20; x++)
		{
			System.out.println("Thread Working on Number: "+x);
			
			//Sleep for 10 seconds to show it running in background
			try
			{
				Thread.sleep(10000);
			}
			catch(Exception ex) 
			{
				System.out.println("Exception:"+ex.toString());
			}
		}
	}
}


Java Runnable Thread Sample Code

The sample code below is the full Java source code that creates and start both the Thread and Runnable classes. In the output below, you can see both of these thread options are running concurrently.

package com.jcd.java.tutorials;

public class JavaThreadExample 
{

	//Our Main Method
	public static void main(String[] args) 
	{

		System.out.println("Start Our Fancy New Runnable");
		
		//Create an instance of our JavaCodeDepotThread Class
		Thread jcdRunnable = new Thread(new JavaCodeDepotRunnable());
		
		//Start our thread and let it run in the background
		jcdRunnable.start();
		
		System.out.println("Start Our Fancy New Thread");
		
		//Create an instance of our JavaCodeDepotThread Class
		Thread jcdThread = new Thread(new JavaCodeDepotThread());
		
		//Start our thread and let it run in the background
		jcdThread.start();


	}

}

//Very Simple Class That Implements Runnable
class JavaCodeDepotRunnable implements Runnable
{
	
	//Put your tasks inside the run() method
	public void run()
	{
		//Print blank line
		System.out.println("");
		
		//Loop for 1 through 10
		for(int x=1; x<=10; x++)
		{
			System.out.println("Runnable Working on Number: "+x);
			
			//Sleep for 10 seconds to show it running in background
			try
			{
				Thread.sleep(10000);
			}
			catch(Exception ex) 
			{
				System.out.println("Exception:"+ex.toString());
			}
		}
	}
}

//Very Simple Class That Extending Thread Class
class JavaCodeDepotThread extends Thread
{
	//Empty Constructor
	public JavaCodeDepotThread()
	{
		super();
	}
	
	//Put your tasks inside the run() method
	public void run()
	{
		//Print blank line
		System.out.println("");
		
		//Loop for 111 through 20
		for(int x=11; x<=20; x++)
		{
			System.out.println("Thread Working on Number: "+x);
			
			//Sleep for 10 seconds to show it running in background
			try
			{
				Thread.sleep(10000);
			}
			catch(Exception ex) 
			{
				System.out.println("Exception:"+ex.toString());
			}
		}
	}
}
Start Our Fancy New Runnable
Start Our Fancy New Thread


Thread Working on Number: 11
Runnable Working on Number: 1
Thread Working on Number: 12
Runnable Working on Number: 2
Runnable Working on Number: 3
Thread Working on Number: 13
Runnable Working on Number: 4
Thread Working on Number: 14
Runnable Working on Number: 5
Thread Working on Number: 15
Thread Working on Number: 16
Runnable Working on Number: 6
Thread Working on Number: 17
Runnable Working on Number: 7
Runnable Working on Number: 8
Thread Working on Number: 18
Runnable Working on Number: 9
Thread Working on Number: 19
Thread Working on Number: 20
Runnable Working on Number: 10



Concluding Comments

While you can create a thread by extending the Java Thread class, the preferred method is to create a thread by implementing the Java Runnable Interface.



Java References:

Add a Comment

Your email address will not be published. Required fields are marked *