线程创建方式

1.继承Thread

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description: 线程创建
 * @author: zfh
 * @version: 1.0
 * @date: 2022/01/14 15:35:26
 **/
public class ThreadDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start.....");
        //方式1
        Thread thread = new Thread01();
        thread.start();

        System.out.println("main......end.....");
    }


    /**
     * 创建线程方式1---继承Thread
     * 没有返回值,不能控制资源,可能导致资源耗尽
     */
    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
        }
    }

}

运行结果
在这里插入图片描述

2.实现Runnable

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description: 线程创建
 * @author: zfh
 * @version: 1.0
 * @date: 2022/01/14 15:35:26
 **/
public class ThreadDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start.....");

        //方式2
        Runable01 runable01 = new Runable01();
        new Thread(runable01).start();

        System.out.println("main......end.....");
    }

    /**
     * 创建线程方式2---实现Runnable
     * 没有返回值,不能控制资源,可能导致资源耗尽
     */
    public static class Runable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
        }
    }

}

运行结果:
在这里插入图片描述

3.实现Callable

import java.util.concurrent.*;

/**
 * @description: 线程创建
 * @author: zfh
 * @version: 1.0
 * @date: 2022/01/14 15:35:26
 **/
public class ThreadDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start.....");
 
        //方式3
        FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
        new Thread(futureTask).start();
        //等待整个线程执行完成,获取返回结果
        System.out.println(futureTask.get());

        System.out.println("main......end.....");
    }

    /**
     * 创建线程方式3---实现Callable
     * 有返回值,不能控制资源,可能导致资源耗尽
     */
    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
            return i;
        }
    }
}

运行结果:
在这里插入图片描述

4.使用线程池

import java.util.concurrent.*;

/**
 * @description: 线程创建
 * @author: zfh
 * @version: 1.0
 * @date: 2022/01/14 15:35:26
 **/
public class ThreadDemo {
    //当前系统中池只有一两个,每个异步任务,提交给线程池让他去执行
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main......start.....");
 
        /**
         *  方式4  使用线程池  可以控制资源
         * 以上3种启动线程以后在业务中都不用,将所有的多线程异步任务都交给线程池执行
         */
        executorService.execute(new Runable01());

        System.out.println("main......end.....");
    }

}

运行结果:
在这里插入图片描述

5.总结

  • 方式1没有返回值,不能控制资源,可能导致资源耗尽
  • 方式2没有返回值,不能控制资源,可能导致资源耗尽
  • 方式3有返回值,不能控制资源,可能导致资源耗尽
  • 以上3种启动线程以后在业务中都不用,将所有的多线程异步任务都交给线程池执行,方式4可以控制资源
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值