package com.business.thread;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/*
*创建线程和启动线程的三种方式:
*1、继承Thread
*2、继承Runnable
*3、继承Callable
*4、线程安全的停止
*/
public class NewThread {
private static class extentsThread extends Thread{
public void run() {
super.run();
System.out.println(Thread.currentThread().getName());
}
}
private static class implementsRunnable implements Runnable{
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
private static class implementsCallable implements Callable<String>{
public String call() throws Exception {
return Thread.currentThread().getName();
}
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
extentsThread thread1 = new extentsThread();
thread1.start();
//interrupt
thread1.interrupt();
implementsRunnable thread2 = new implementsRunnable();
new Thread(thread2).start();
implementsCallable thread3 = new implementsCallable();
FutureTask<String> futureTask = new FutureTask<>(thread3);
new Thread(futureTask).start();
String result = futureTask.get();
System.out.println(result);
}
}