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

JavaTutorOnline

Java Tutor Online

  • Home
  • 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

Java
1
2
3
4
5
6
7
8
9
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

Java
1
2
3
4
5
6
7
8
9
10
11
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

Java
1
2
3
4
5
6
7
8
9
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

Java
1
2
3
4
5
6
7
8
9
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

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

Java
1
2
3
4
5
6
7
8
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
[email protected]
Skype id: p.chinmay

Recent Posts

  • 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
  • How to Learn Java in One day? | JavaTutorOnline
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse
  • How to learn Java Programming Language the Best way in 2025?
Copyright © 2025 JavaTutorOnline