多线程实现的几种方式

1.继承Thread类

public class ThreadDemo1 extends Thread {

    public ThreadDemo1() {
    }

    @Override
    public void run() {
        System.out.println("继承Thread类实现多线程");
    }

    public static void main(String[] args) {
        // 子类 start.
        ThreadDemo1 threadDemo1 = new ThreadDemo1();
        threadDemo1.start();

        // Thread 类 start.
        Thread thread = new Thread(threadDemo1);
        thread.start();

    }
}

输出结果

多线程实现的几种方式_第1张图片

2.实现Runnable接口

public class MyThread implements Runnable {

    @Override
    public void run() {
        System.out.println("实现Runnable接口实现多线程");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new MyThread());
        thread.start();
    }
}

3.通过Callable和FutureTask创建线程

public class ThreadDemo2 implements Callable {

    @Override
    public String call() throws Exception {
        System.out.println("通过Callable和FutureTask创建线程 ");
        return "通过FutureTask对象的get";
    }

    public static void main(String[] args) throws Exception {
        Callable callable = new ThreadDemo2();
        FutureTask task = new FutureTask<>(callable);

        Thread thread = new Thread(task);
        thread.start();
        Thread.sleep(10);
        // 通过FutureTask对象的get()方法得到返回值
        System.out.println(task.get());
    }
}

4.通过原生线程池创建线程

@Slf4j
public class ThreadPoolExample1 {

    public static void main(String[] args) {

        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < 10; i++) {
            executorService.execute(() -> log.info("task:{}", Thread.currentThread().getName()));
        }
        executorService.shutdown();
    }

}

5.Spring 自带线程池

public class ThreadDemo3 {

    public static void main(String[] args) throws Exception {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(8);
        executor.setMaxPoolSize(8);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.setAllowCoreThreadTimeOut(true);
        executor.setThreadNamePrefix("spring thread pool");
        executor.initialize();

        // 1. 通过 execute 方法 ,传入一个Runnable对象
        executor.execute(() -> {
            System.out.println("不带返回值");
        });

        // 2.通过submit 方法,传入一个Callable 对象
        Future callFuture = executor.submit(new Callable() {
            @Override
            public Object call() {
                System.out.println("通过submit 方法,传入一个Callable 对象");
                return "返回结果 - 通过submit 方法,传入一个Callable 对象";
            }
        });
        System.out.println(callFuture.get());

        Future callFutureLambda = executor.submit(() -> {
            System.out.println("通过submit 方法,传入一个Callable 对象");
            return "hello";
        });

        // 3. 通过 submit 方法,传入一个Runnable 对象
        Future runnableFuture = executor.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("通过 submit 方法,传入一个Runnable 对象");
            }
        });
        System.out.println(runnableFuture.get());
        
        Future runnableFutureLambda = executor.submit(() -> System.out.println("通过 submit 方法,传入一个Runnable 对象"));


    }
} 
  

多线程实现的几种方式_第2张图片

你可能感兴趣的:(Java多线程)