Table of contents
Option 1: Inherit the Thread class
Option 2: Implement by implementing the Runnable interface
Option 3: Implement using Callable and Future interfaces
Option 1: Inherit the Thread class
- Define a class MyThread that inherits the Thread class
- Override the run() method in the MyThread class
- Create an object of class MyThread
- Start thread
Step 1: Define a class MyThread that inherits the Thread class package com.java.factory.TheadDemo; public class MyThread extends Thread{ //Rewrite the run method in Thread @Override public void run() { for (int i = 0; i <20 ; i++) { System.out.println("Thread thread is started"+i); } } }Step 3: Create an object of MyThread class
package com.java.factory.TheadDemoText; import com.java.factory.TheadDemo.MyThread; public class MyThreadText { public static void main(String[] args) { MyThread myThread = new MyThread(); //Start thread myThread.start(); } }
Option 2: Implement by implementing the Runnable interface
- Define a class MyRunnable to implement the Runnable interface
- Override the run() method in the MyRunnable class
- Create MyRunnbale class object
- Create an object of the Thread class and use the MyRunnable object as a parameter of the constructor method
- Start thread
Step 1: Define a class MyRunnable to implement the Runnable interface package com.java.factory.TheadDemo; public class MyRunnable implements Runnable { //Rewrite the run() method @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("Runnable thread starts"); } } }Step 3: Create MyRunnbale class object
package com.java.factory.TheadDemoText; import com.java.factory.TheadDemo.MyRunnable; public class MyRunnableText { public static void main(String[] args) { //Create parameter object MyRunnable myR = new MyRunnable(); //Create a thread object and pass parameters to the thread //The thread starts and the run method in the parameters is executed. Thread thread = new Thread(myR); //Start thread thread.start(); } }
Option 3: Implement using Callable and Future interfaces
- Define a class MyCallable to implement the Callable interface
- Override the call() method in the MyCallable class
- Create an object of MyCallable class
- Create a FutureTask object of the Future implementation class and use the MyCallable object as a parameter of the constructor
- Create an object of the Thread class and use the FutureTask object as a parameter of the constructor method
- Start thread
Step 1: Define a class MyCallable to implement the Callable interface
package com.java.factory.TheadDemo; import java.util.concurrent.Callable; public class MyCallable implements Callable<String> { @Override public String call() throws Exception { for (int i = 0; i < 10; i++) { System.out.println("MyCallable is hungry"); } //The return value represents the result after the thread completes running return "Order takeout!"; } }Step 3: Create an object of MyCallable class
package com.java.factory.TheadDemoText; import com.java.factory.TheadDemo.MyCallable; import java.util.concurrent.FutureTask; public class MyCallableText { public static void main(String[] args) { //Create object and execute call method MyCallable myCallable = new MyCallable(); //You can get the result after the thread execution is completed, or you can pass it to the Thread object as a parameter FutureTask<String> FT = new FutureTask<>(myCallable); //Create thread object Thread thread = new Thread(FT); //Start thread thread.start(); } }