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.
Step 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
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
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
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
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
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
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"); } } } |
Leave a Reply