Java 多线程学习 - notilluss/zhfbigdata-javase GitHub Wiki

import java.util.concurrent.*;

public class HowToCreateThread { //线程创建方法有是三个

//第一种:继承Thread类
static class myThread extends Thread{
    @Override
    public void run(){
        System.out.println("Hello  MyThread!!!");
    }
}

//第二种:实现Runnable接口
static class myRun implements Runnable{
    @Override
    public void run(){
        System.out.println("Hello  MyRun!!!");
    }
}

//第三种:实现Callable接口,同时实现此接口的类要么是抽象的,要么实现call()方法
static class Mycall implements Callable<String>{
    @Override
    public String call(){
        System.out.println("Hello  MyCall!!!");
        return "success";
    }
}

public static void main(String[] args) {
    //启动线程的五种方式

    //第一种========================
    new myThread().run();

    //第二种=========================
    new Thread(new myRun()).start();

    //第三种===========================
    new Thread(()->{
        System.out.println("Hello Lambda!");
    }).start();

    //第四种============================
    Thread t = new Thread(new FutureTask<String>(new Mycall()));
    t.start();

    //第五种============================
    ExecutorService service = Executors.newCachedThreadPool();
    service.execute(()->{
        System.out.println("Hello  ThreadPool!!!");
    });
    service.shutdown();
}

}

⚠️ **GitHub.com Fallback** ⚠️