Use Java multi-threaded There are three ways summary

Use Java multi-threaded There are three ways Summary:

Thread class inheritance

Implement Runnable

And create threads using Callable and Future

This article will introduce one by one these three methods.

1, inheritance Thread class


public class MyThread extends Thread {
    @Override
    public void run(){
        super.run();
        System.out.println("执行子线程...");
    }
}
public class Test {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
        System.out.println("主线程...");
    }
}

2, implement Runnable


public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("执行子线程...");
    }
}
public class Test {
    public static void main(String[] args) {

        Runnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
        System.out.println("主线程运行结束!");
    }
}

3, using the Callable and Future create a thread

import java.util.concurrent.Callable;

public class MyCallable implements Callable {
    int i = 0;
    @Override
    public Object call() throws Exception {
        System.out.println(Thread.currentThread().getName()+"  i的值:"+ i);
        return i++; //call方法可以有返回值
    }
}
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class Test {
    public static void main(String[] args) {
        Callable callable = new MyCallable();
        for (int i = 0; i < 10; i++) {
            FutureTask task = new FutureTask(callable);
            new Thread(task,"子线程"+ i).start();
            try {
                //获取子线程的返回值
                System.out.println("子线程返回值:"+task.get() + "\n");
            }  catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

5 contrast, three ways

The first two and the following comparison:

1, the code can be seen, the first method is the most simple and convenient, can directly start, without any conversion
. 2, the first place is a very good due to a single inherited after the Thread class java inheritance mechanism , can not inherit from other classes, while if the implementation of the interface, you can implement multiple interfaces, allowing developers more flexibility.

The second and third way comparison:

1, Similarly, the third embodiment is the second method is relatively simple code more, more convenient, less time converter
2, the third method has two advantages: the return value wait for the results can be thrown

to sum up:

First and second, they are to run method overrides or implements, and are thread asynchronous

A third implementation call method, and the sub-synchronous asynchronous thread main thread wait for the results set

supplement

Lambda use in java1.8 can easily achieve a thread

new Thread(()-> 
       EmailUtil.sendWorkbookGropBuy(gropBuyService.creatGropBuy(businessId,cardId ,userId         
       ,count),user.getEmail())
).start();

 

Published 24 original articles · won praise 0 · Views 404

Guess you like

Origin blog.csdn.net/WillliveWillWork/article/details/101367962
Recommended