Java multithreading study notes _ multithreading implementation

Multi-threaded implementation scheme:

  1. It is implemented by inheriting the Thread class.
  2. Implementation of the Runnable interface.
  3. Use Callable interface and Future interface to achieve.

Option 1: Inherit the Thread class

  1. Define a MyThread class to inherit the Thread class.
  2. Override the run() method in the MyThread class.
  3. Create an object of the MyThread class.
  4. Start the thread.
public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("线程开始了" + i);
        }
    }
}


public class Demo {
    public static void main(String[] args) {
        MyThread m1 = new MyThread();
        MyThread m2 = new MyThread();
        m1.start();
        m2.start();
    }
}

  • Why rewrite the run() method?

Because run() is used to encapsulate the code that the thread needs to execute.

  • What is the difference between the run() method and the start() method?

run() means only to create an object, call the method of the object, and does not start the thread.

start() means to interact with system resources and start a thread.


 

 Method 2: Implement the Runnable interface

  1. Define a MyRunnable class to implement the Runnable interface.
  2. Override the run() method in the MyRunnable class.
  3. Create an object of the MyRunnable class.
  4. Create an object of the Thread class and pass the object of the MyRunnable class as a parameter of the constructor.
  5. Start the thread.

 

public class MyRunnable implements Runnable{
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("第二种方法实现多线程" + i);
        }
    }
}

public class Demo {
    public static void main(String[] args) {

        MyRunnable mr1 = new MyRunnable();//创建了一个参数的对象。
        Thread t1 = new Thread(mr1);//创建了一个线程对象,并把参数传递给这个线程。
        t1.start();//在线程启动之后执行的是参数里面的run()方法。

        MyRunnable mr2 = new MyRunnable();
        Thread t2 = new Thread(mr2);
        t2.start();
    }
}

 

 Method 3: Implement Callable interface and Future interface

  1. Define the MyCallable class to implement the Callable interface.
  2. Override the call() method in the MyCallable class.
  3. Create an object of the MyCallable class
  4. Create an object of the FutureTask class, which is the implementation of the Future class, and pass the object of the MyCallable class 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.
  6. Start the thread.
//这里的泛型中的数据类型和下面call()方法的返回值类型一致
//想要返回什么数据,就写对应的数据类型
public class MyCallable implements Callable<String> {

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 100; i++) {
            System.out.println("第" + i + "次表白");
        }
        return "I DO.";
    }
}

public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //线程开启后需要执行里面的call()方法        
        MyCallable mc = new MyCallable();
        //好比一个中间媒介
        //既可以获取线程执行完毕的结果,也可以作为参数传递给Thread对象。
        FutureTask<String> ft = new FutureTask<>(mc);
        //创建线程对象        
        Thread t = new Thread(ft);
        //开启线程
        t.start();
        System.out.println(ft.get());
    }
}

 The FutureTask class implements both the Future interface and the Runnable interface. You can check the official documentation for details, so it can be passed into the Thread construction method as a parameter.

Note: The get() method should be called after the thread is started.


Comparison of three ways

 

  advantage Disadvantage
Implement the interface Implement Runnable interface (no return value) Extensibility is strong, and other classes can be inherited while implementing this interface. Programming is relatively complicated, and the methods in the Thread class cannot be used directly.
Implement Callable interface (with return value)
Inherit the Thread class

Programming is relatively simple,

You can directly use the methods in the Thread class.

 

Poor scalability,

Can no longer inherit other classes.

 

 

Guess you like

Origin blog.csdn.net/qq_43191910/article/details/114818828