Multi-threaded implementation

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

  1. Define a class MyThread that inherits the Thread class
  2. Override the run() method in the MyThread class
  3. Create an object of class MyThread
  4. 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

  1. Define a class MyRunnable to implement the Runnable interface
  2. Override the run() method in the MyRunnable class
  3. Create MyRunnbale class object
  4. Create an object of the Thread class and use the MyRunnable object as a parameter of the constructor method
  5. 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

  1. Define a class MyCallable to implement the Callable interface
  2. Override the call() method in the MyCallable class
  3. Create an object of MyCallable class
  4. Create a FutureTask object of the Future implementation class and use the MyCallable object as a parameter of the constructor
  5. Create an object of the Thread class and use the FutureTask object as a parameter of the constructor method
  6. 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();

    }
}

 

 

Guess you like

Origin blog.csdn.net/qq_51358596/article/details/127374374