• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
Online Java Training-Online Java Tutor-Private Java Classes

Online Java Training-Online Java Tutor-Private Java Classes

Personal Online Java Training through Skype

  • 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

Phone & Whatsapp +919853166385
[email protected]
Skype id: p.chinmay

Recent Posts

  • 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 ? | JavaTutorOnline
  • Recursion in Java Example Program | Understanding Java Recursion
  • Important Core Java Interview Questions and Answers
  • How to Create Threads in Java | Multithreading in Java | JavaTutorOnline
  • Why Multiple Inheritance in Java not supported?
  • A Simple Spring Mvc Hello World Example | Spring Mvc Tutorial
  • How to become a good Teacher |Ideal Teacher | Perfect Teacher
  • OGNL in Struts 2 Tutorial | JavaTutorOnline
Copyright © 2022 JavaTutorOnline