总结线程的创建

package com.syn;

import java.util.concurrent.*;

//回顾总结线程的创建
public class ThreadNew {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        new MyThread1().start();

        new Thread(new MyThread2()).start();

//        MyThread3 thread3 = new MyThread3();
//        //创建执行服务
//        ExecutorService service = Executors.newFixedThreadPool(1);//创建一个拥有1个线程的线程池
//        Future<Boolean> submit = (Future<Boolean>) service.submit(thread3);//提交执行
//        Boolean s = submit.get();//获取结果
//        System.out.println(s);
//        service.shutdownNow();//关闭服务


        FutureTask<Integer> futureTask = new FutureTask<Integer>(new MyThread3());
        new Thread(futureTask).start();

        Integer integer = futureTask.get();
        System.out.println(integer);



    }
}

//1.继承Thread类
class MyThread1 extends Thread{
    @Override
    public void run() {
        System.out.println("MyThread1");
    }
}

//2.实现Runnable接口
class MyThread2 implements Runnable{
    @Override
    public void run() {
        System.out.println("MyThread2");
    }
}

//3.实现Callable接口
class MyThread3 implements Callable {
    @Override
    public Integer call() throws Exception {
        System.out.println("MyThread3");
        return 100;
    }
}
posted @ 2022-04-28 18:06  好12  阅读(25)  评论(0)    收藏  举报