Video: https://edu.aliyun.com/course/36/learn?spm=5176.8764728.0.0.fVZ5cb#lesson/433
1. Starting with JDK1.5, a new development package has been added: Java.util.concurrent, the development package is primarily for high-performance programming, which provides classes that will be used in high concurrency operations in this development package. Within this package there is a new interface defined:
Public Interface Callable<v> { publicthrows Exception;}
Although the run () method in Runnable is also the main method of the thread, it does not return a value because its design follows the principle of the main method, that is, the thread starts and does not look back. However, there are a number of times when a return value is required, such as when certain threads are executed, which can result in some return, which can only be done through callable. (Description in JDK: Thecallable interface is similar in Runnable
that both are designed for classes whose instances might be executed by another thread.) However, Runnable does not return a result and cannot throw a checked exception. )
Example: Defining a thread principal class using callable
class Implements Callable<string>{ @Override publicthrows Exception { for (int x=0;x<20;x++) { System.out.println ("Sell ticket, X" +x); } return "The ticket is sold out"; }} // this is the principal class of a thread.
public class MyThread222 {
public static void Main (string[] args) {
}
}
In either case, if you want to start multithreading only the Thread class's start () method,
Multithreading-through callable implementation (in)