There are two ways for JDK to implement multi-threaded programming: implementing the Runnable interface and implementing the Callable interface

In terms of interface-oriented design principles, there are two ways to implement multi-threaded programming: implement the Runnable interface; implement the Callable interface.

Implement the Runnable interface

There are two common ways to implement multi-threaded programming: implementing the Runnable interface; inheriting the Thread parent class. The Thread thread class is the implementation class of the Runnable interface. The java class does not support multi-root inheritance. The class can implement multiple interfaces, and the interfaces support multi-root inheritance. Therefore, it is recommended and only discussed here to implement the Runnable interface. programming.

1. Define the thread: implement the interface Runnable, and rewrite the entry method of the thread: run()

public class MyRunnableThread implements Runnable{

    public String taskName;

    public MyRunnableThread(String taskName) {
        super();
        this.taskName = taskName;
    }

    @Override
    public void run() {
        System.out.println(taskName + " 被执行");
    }

}

2. Running thread: thread MyRunnableThread as a constructor parameter to build a thread running class: Thread. Thread class method: start () to start the thread.

public static void main(String[] args) {
    int threadSize = 5;
    for(int i = 0;i < threadSize;i++) {
        // Thread类的方法启动线程,
        new Thread(new MyRunnableThread("线程" + i)).start();
    }
}
// 执行结果如下,可以看出线程执行的无序性
线程2 被执行
线程0 被执行
线程1 被执行
线程3 被执行
线程4 被执行

Implement the Callable interface

The difference between the thread that implements the Callable interface and the thread that implements the Runnable interface is that the thread of the latter has a return value , and the return value is encapsulated by the Future class. The return value of the thread can be obtained through the method of the Future class: get().

1. Define the thread: implement the Callable interface and rewrite the entry method of the thread: call()

public class MyCallableThread implements Callable<Object>{

    private String taskName;

    public MyCallableThread(String taskName) {
        super();
        this.taskName = taskName;
    }

    @Override
    public Object call() throws Exception {
        System.out.println(taskName + " 被执行");
        return taskName;
    }

}

2. It is recommended to run MyCallableThread as a thread pool ExecutorService

public static void main(String[] args) throws InterruptedException, ExecutionException {
    int taskSize = 5;
    // 线程池的方式,执行带有返回值的线程(实现接口Callable):MyCallable
    // 线程池
    ExecutorService executorService = Executors.newFixedThreadPool(taskSize);
    // 返回封装结果(Future<Object>)的集合
    List<Future<Object>> futures = new ArrayList<>(taskSize);
    for(int i = 0;i < taskSize;i++) {
        futures.add(executorService.submit(new MyCallableThread("线程 :" + (i + 1))));
    }
    // 关闭线程池
    executorService.shutdown();
    Thread.sleep(1000);// 暂停1秒
    for (Future<Object> future : futures) {
        System.out.println(future.get());
    }
}

Guess you like

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