• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

1-on-1 Online Java Training by a Senior Software Engineer

  • Home
  • AP CSA
    • FRQ Practice
      • Arrays
      • ArrayList
      • Strings
      • 2D Arrays
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

How to Create Threads in Java

Threads can be created in java using two techniques. By implementing the Runnable interface or by extending the Thread class.

By implementing the runnable interface.

how To Create Threads in JavaStep 1: Create a child class that implements the runnable interface.
Step 2: Provide the working of the thread inside the run method
Step 3: Create another class containing the main function.
Step 4: Inside the main, create an object of the child class and pass it into the threads constructor.
Step 5: Then call the start function on the thread object created. This will invoke the run method defined
in the child class.

Child.java
public class Child implements Runnable {
	@Override
	public void run() {
		// Create an infinite while loop that goes on printing java, java, java.
		while (true) {
			System.out.println("java");
		}
	}
}
Learn details about threads in Java> > Private Online Core Java Training
ThreadMain.java
public class ThreadMain {
	public static void main(String[] args) {
		Child c1 = new Child();// Created an object of the child class
		Thread t1 = new Thread(c1); // Created a thread object who's working has been defined
		// in the child class run method.
		t1.start(); // Then we start the thread.
		while (true) {
			System.out.println("j2ee");
		}
	}
}

By extending the Thread class.

Step 1: Create a child class that extends the Thread class.
Step 2: Provide the working of the thread inside the run method
Step 3: Create another class containing the main function.
Step 4: Inside the main, create an object of the child class.
Step 5: Then call the start function on the child object created. This will invoke the run method defined
in the child class.

Child.java
public class Child extends Thread {
	@Override
	public void run() {
		// Create an infinite while loop that goes on printing java, java, java.
		while (true) {
			System.out.println("java");
		}
	}
}
ThreadMain.java
public class ThreadMain {
	public static void main(String[] args) {
		Child c1 = new Child();// Created an object of the child class
		c1.start(); // Then we start the thread.
		while (true) {
			System.out.println("j2ee");
		}
	}
}

 

Which technique should be preffered to create Threads in Java?

We should prefer implementing Runnable rather than extending thread, because multiple inheritance is not supported through classes but it is supported through interfaces.

class XYZ extends Applet, Thread not possible
class XYZ extends Applet implements Runnable is possible

Decoupled Good Java Code for Creating Threads

Child.java
public class Child implements Runnable {
	Child() {
		Thread t1 = new Thread(this); // Created a thread object who's working has been defined
		// in the child class run method.
		t1.start(); // Then we start the thread.
	}
	@Override
	public void run() {
		// Create an infinite while loop that goes on printing java, java, java.
		while (true) {
			System.out.println("java");
		}
	}
}
ThreadMain.java
public class ThreadMain {
	public static void main(String[] args) {
		new Child();
		while (true) {
			System.out.println("j2ee");
		}
	}
}

 

Filed Under: Java

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Mr Chinmay

Chinmay Patel
Online Java Tutor-Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • How to Learn Java in One day? | Crash Course | JavaTutorOnline
  • Constructor in Java and Overloaded Constructor Example Program
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse
Copyright © 2026 JavaTutorOnline