Multi-threaded study notes (2)--three ways to create threads

introduce

 There are two main ways to create threads. First, inherit the Thread class and override the run() method. Second, implement the Runnable interface and override the run() method. In addition, there is another way to implement the callable interface and override the run() method. call() method.

Inherit from the Thread class

 Disadvantages: The feature of the java language is to support single inheritance, so this method is not conducive to expansion.

/**
 * Step 1: Inherit the Thread class
 * Step 2: Rewrite the run method
 * Step 3: new corresponding class object
 * Step 4: Call the start() method of the object of this class
 */
public class MyThread extends Thread{
    private int count = 5;

    public synchronized void run(){
        count--;
        System.out.println(this.currentThread().getName() + "count =" + count);
    }

    public static void main(String[] args) {
// new corresponding class object
        MyThread myThread = new MyThread();
        //The first way to write, try to unpack the comment
        //myThread.start();
        //Second way of writing
        Thread t1 = new Thread(myThread,"t1");
        //call start method
        t1.start();
    }
}

Implement the Runnable interface

Advantages: The nature of implementation is the same as inheriting the Thread class. Java can implement multiple interfaces, which is more flexible.

/**
 * Step 1: Implement the Runnable interface
 * Step 2: Implement the run() method
 * Step 3: Create a class object
 * Step 4: The class object calls the start() method
 */
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "实现Runnable方式");
    }

    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread t1 = new Thread(myRunnable,"t1");
        t1.start();
    }
}

Implement the callable interface

Features: In this method, it must be used in conjunction with Future. Compared with the above two methods, the above two methods have no return value, and the method implementing the callable interface supports return values.

public class MyFuture {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //The first step: create a FutureTask object through the Callable interface
        FutureTask<String> ft = new FutureTask<String>(new Callable<String>() {
            //Step 2: Override the call() method
            @Override
            public String call() throws Exception {
                return Thread.currentThread().getName();
            }
        });
        //Step 3: Create thread t1
        Thread t1 = new Thread(ft);
        //Step 4: Start the thread
        t1.start();
        //Step 5: Obtain the return value of the call() method through the get() method
        String result = ft.get();
        System.out.println(result);
    }
}

The difference between run() and start() startup

 The above examples are all threads started by calling the start() method of the thread. After the thread calls start(), the start() method will create a new thread and let the thread execute the run() method. The run() method will not create a new thread, causing the program to be executed serially. Therefore, it is recommended to use the start() method to start threads instead of run(). Examples can be found here: https://blog.csdn.net/kapukpk/article/details/53008516

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325935668&siteId=291194637