Java8 - three multi-threading methods

1. How to use multithreading

1.1. Inherit the Thread class

        Inheriting Thread and overriding the run() method, the start method in the Thread class will call the system method to execute the corresponding thread. In fact, Thread also implements the Runable interface, which we can find from the documentation of this class.

1.2. Implement the Runable interface

        Implement the Runable interface and override the run() method. To start a thread, you must use the start() method of the Thread class.

1.3. Implement the Callable interface

        A code example that implements the Callable interface:

import java.util.concurrent.Callable;   
import java.util.concurrent.ExecutorService;   
import java.util.concurrent.Executors;   
import java.util.concurrent.Future;   
  
/** *//**  
 * Callable 和 Future接口  
 * Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类都是可被其它线程执行的任务。  
 * Callable和Runnable有几点不同:  
 * (1)Callable规定的方法是call(),而Runnable规定的方法是run().  
 * (2)Callable的任务执行后可返回值,而Runnable的任务是不能返回值的。  
 * (3)call()方法可抛出异常,而run()方法是不能抛出异常的。  
 * (4)运行Callable任务可拿到一个Future对象,  
 * Future 表示异步计算的结果。它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。  
 * 通过Future对象可了解任务执行情况,可取消任务的执行,还可获取任务执行的结果。  
 */  
public class CallableAndFuture {   
  
    /** *//**  
     * 自定义一个任务类,实现Callable接口  
     */  
    public static class MyCallableClass implements Callable{   
        // 标志位   
        private int flag = 0;   
        public MyCallableClass(int flag){   
            this.flag = flag;   
        }   
        public String call() throws Exception{   
            if (this.flag == 0){   
                // 如果flag的值为0,则立即返回   
                return "flag = 0";   
            }    
            if (this.flag == 1){   
                // 如果flag的值为1,做一个无限循环   
                try {   
                    while (true) {   
                        System.out.println("looping.");   
                        Thread.sleep(2000);   
                    }   
                } catch (InterruptedException e) {   
                    System.out.println("Interrupted");   
                }   
                return "false";   
            } else {   
                // falg不为0或者1,则抛出异常   
                throw new Exception("Bad flag value!");   
            }   
        }   
    }   
       
    public static void main(String[] args) {   
        // 定义3个Callable类型的任务   
        MyCallableClass task1 = new MyCallableClass(0);   
        MyCallableClass task2 = new MyCallableClass(1);   
        MyCallableClass task3 = new MyCallableClass(2);   
           
        // 创建一个执行任务的服务   
        ExecutorService es = Executors.newFixedThreadPool(1);   
        try {   
            // 提交并执行任务,任务启动时返回了一个 Future对象,   
            // 如果想得到任务执行的结果或者是异常可对这个Future对象进行操作   
            Future future1 = es.submit(task1);   
            // 获得第一个任务的结果,如果调用get方法,当前线程会等待任务执行完毕后才往下执行   
            System.out.println("task1: " + future1.get());   
               
            Future future2 = es.submit(task2);   
            // 等待5秒后,再停止第二个任务。因为第二个任务进行的是无限循环   
            Thread.sleep(5000);   
            System.out.println("task2 cancel: " + future2.cancel(true));   
               
            // 获取第三个任务的输出,因为执行第三个任务会引起异常   
            // 所以下面的语句将引起异常的抛出   
            Future future3 = es.submit(task3);   
            System.out.println("task3: " + future3.get());   
        } catch (Exception e){   
            System.out.println(e.toString());   
        }   
        // 停止任务执行服务   
        es.shutdownNow();   
    }   
}

2. Thread synchronization

2.1. Synchronous code block

synchronized("lock"){ //There are usually two kinds of locks here, one is a string constant, and the other is this

        //Code block that needs to be executed synchronously
}

2.2, synchronization method

public synchronized void xxx(){
        //synchronized method
}

3. Summary

3.1. The start of all threads must call the start() method in the Thread class

new Thread("Thread name").start();

3.2. Implementing Runable is the first method that should be considered, for the following two reasons

  • Inheriting classes in Java has limitations, each class can only inherit one class, but can implement multiple interfaces
  • Implementing the Runable interface can better share data
  • Implementing Callable can return a value (the other two methods cannot return a value after the thread ends)

 

 Reprinted from:

        https://blog.csdn.net/jpzhu16/article/details/53326506

        https://my.oschina.net/rouchongzi/blog/129333

This article has a new typesetting, and the text information mainly comes from the above link.

Guess you like

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