CompletableFuture 用法详解解

2023年8月9日修改
原理介绍
Java文档说明,创建线程的方式只有两种:继承 Thread 或者实现 Runnable 接口,具体源码说明如下
2023.08.09 修改:创建线程应该有4四种方法:在 Thread 类中只说了两种最基本的
Add:
1.还可以实现 callable 接口(有返回值)
2.还可以通过线程池创建
代码块
// 继承 Thread 类
class PrimeThread extends Thread {
long minPrime;
PrimeThread(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
 . . .
}
}
// The following code would then create a thread and start it running:
PrimeThread p = new PrimeThread(143);
p.start();
// 实现 Runnable 接口
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}
public void run() {
// compute primes larger than minPrime
 . . .
}
}
// The following code would then create a thread and start it running:
PrimeRun p = new PrimeRun(143);
new Thread(p).start();
由上述代码可以在对应线程执行 run 方法以后返回值为 void