线程的三种调用方法

线程的三种调用方法
一:调用直接继承Thread
二:实现Runnable接口
三:实现callable接口

package Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

//线程总结
public class ThreadInclude {
    public static void main(String[] args) {
        new MyThread1().start();
        
        new Thread(new MyThread2()).start();
        
        FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread3());
        new Thread(futureTask).start();
        try {
            Integer integer = futureTask.get();
            System.out.println(integer);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}

class MyThread1 extends Thread{
    @Override
    public void run() {
        System.out.println("MyThread1");
    }
}

class MyThread2 implements Runnable{
    @Override
    public void run() {
        System.out.println("MyThread2");
    }
}
class MyThread3 implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        return 100;
    }
}

你可能感兴趣的:(Java)