New Thread with Runnable

With this example we are going to demonstrate how to create a Thread with a Runnable. We have created a Thread with a Runnable as described below:

  • We have created ThreadWithRunnableExample that implements Runnable and overrides its run() API method. In this method the Runnable’s thread sleeps using sleep(long millis) API method of Thread.
  • We create a new Thread with this runnable, and all its start() method so that its execution begins.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

public class ThreadWithRunnableExample implements Runnable {
	public static void main(String[] args) {
		Thread t = new Thread(new ThreadWithRunnableExample(), "Child Thread");
		for (int i = 0; i < 2; i++) {

			System.out.println("Main thread : " + i);

			try {
			} catch (InterruptedException ie) {
				System.out.println("Main thread interrupted! " + ie);
		System.out.println("Main thread finished!");

	public void run() {
		for (int i = 0; i < 2; i++) {
			System.out.println("Child Thread : " + i);

			try {
			} catch (InterruptedException ie) {
				System.out.println("Child thread interrupted! " + ie);

		System.out.println("Child thread finished!");



Main thread : 0
Child Thread : 0
Main thread : 1
Child Thread : 1
Main thread finished!
Child thread finished!

This was an example of how to create a Thread with a Runnable in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button