Home  >  Article  >  Java  >  Is java multi-threaded?

Is java multi-threaded?

(*-*)浩
(*-*)浩Original
2019-11-09 13:44:393269browse

Java is multi-threaded. There are three ways to use multi-threading: inheriting the Thread class, implementing the Runnable interface and using Callable and Future to create threads.

Is java multi-threaded?

##Inherit the Thread class  (Recommended learning: java course  )

Implementation The method is very simple. You only need to create a class to inherit the Thread class and then override the run method. In the main method, call the start method of the class instance object to achieve multi-thread concurrency. Code:

public class MyThread extends Thread {
    @Override
    public void run(){
        super.run();
        System.out.println("执行子线程...");
    }
}

Test case:

public class Test {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
        System.out.println("主线程...");
    }
}

Running result:

Is java multi-threaded?

Implement the Runnable interface

The implementation of this method is also very simple, which is to change the inherited Thread class to implement the Runnable interface. The code is as follows:

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("执行子线程...");
    }
}

Test case:

public class Test {
    public static void main(String[] args) {
        Runnable runnable = new MyRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
        System.out.println("主线程运行结束!");
    }
}

Running result:

Is java multi-threaded?

Use Callable and Future to create threads

The above two methods have these two problems:

The return value of the child thread cannot be obtained

The run method cannot throw exceptions

In order to solve these two problems, we need to use the Callable interface. Speaking of interfaces, the above Runnable interface implementation class instance is passed in as a parameter of the constructor of the Thread class, and then the content in the run method is executed through Thread's start. However, Callable is not a sub-interface of Runnable, but a brand new interface. Its instance cannot be directly passed into the Thread construct, so another interface is needed to convert it.

There is actually one more conversion process than the previous method. In the end, a new thread is created through Thread's start. With this idea, the code is easy to understand:

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方法可以有返回值
    }
}

Test:

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();
            }
        }
    }
}

Execution result (part):

Is java multi-threaded?

The above is the detailed content of Is java multi-threaded?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn