package com.multi.thread;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class App extends Thread {
@Override
public void run() {
// 方法体
this.setName("继承的子线程");
System.out.println(this.getName());
}
public static void main(String[] args) throws InterruptedException {
System.out.println("主线程:" + Thread.currentThread().getName());
// 启动新线程
new App().start();
}
}
class Impl implements Runnable {
@Override
public void run() {
// 方法体
Thread.currentThread().setName("实现的子线程");
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args) {
System.out.println("主线程:" + Thread.currentThread().getName());
// 启动新线程
Impl impl = new Impl();
new Thread(impl).start();
}
}
class Call implements Callable {
@Override
public Integer call() throws Exception {
// 方法体
Thread.currentThread().setName("实现的子线程");
System.out.println(Thread.currentThread().getName());
int i = 1;
return i;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
System.out.println("主线程:" + Thread.currentThread().getName());
Call call = new Call();
FutureTask<Integer> futureTask = new FutureTask<>(call);
new Thread(futureTask).start();
System.out.println(futureTask.get());
}
}