线程Thread和线程池

package com.xxx.xxx.xxx.thread;

import java.util.concurrent.*;

public class ThreadTest {

    public static ExecutorService service = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 1 继承Thread
         *         System.out.println("main.......start......");
         *         Thread01 thread01 = new Thread01();
         *         thread01.start();
         *         System.out.println("main.......end......");
         *
         * 2 实现Runnable接口
         *         System.out.println("main.......start......");
         *         Runnable01 runnable01 = new Runnable01();
         *         new Thread(runnable01).start();
         *         System.out.println("main.......end......");
         *
         * 3 实现Callable接口+FutureTask(可以拿到返回结果,可以处理异常)
         *         System.out.println("main.......start......");
         *         FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
         *         new Thread(futureTask).start();
         *         //阻塞等待整个线程执行完成,获取返回结果
         *         Integer integer = futureTask.get();
         *         System.out.println("main.......end......" + integer);
         *
         * 4 线程池(ExecutorService)
         *         给线程池直接提交任务
         *         service.execute(new Runnable01());
         *         1 创建:
         *                1)Executors
         *                2) new ThreadPoolExecutor
         *
         * 区别:
         *      1、2不能得到返回值。3可以获取返回值
         *      1、2、3都不能到达控制资源的效果
         *      4可以控制资源,性能稳定
         * **/
        //我们以后在业务代码里面,以上三种启动线程的方式都不用。【将所有的多线程异步任务都交给线程池执行】
        //        new Thread(() -> System.out.println("hello")).start();
        //当前系统中现线程池只有一两个,每个异步任务直接提交给线程池,让它自己去执行
        /**
         * 七大参数
         * 1 corePoolSize:[5]核心线程数[一直存在除非(allowCoreThreadTimeOut)];线程池创建好以后就准备就绪的线程数量,就等待来接受异步任务去执行。
         *                5个 Thread thread =new Thread(); thread.start();
         * 2 maximumPoolSize:[200]最大线程数量;控制资源
         * 3 keepAliveTime:存活时间;如果当前的线程数量大于core数量。
         *                 释放空闲的线程(maximumPoolSize-corePoolSize)。只要线程空闲大于指定的keepAliveTime;
         * 4 unit:时间单位
         * 5 BlockingQueue<Runnable> workQueue:阻塞队列;如果任务有很多,就会将目前多的任务放在队列里面。
         *                                     只要有线程空闲,就会去队列里面取出新的任务继续执行。
         * 6 threadFactory:线程的创建工厂。
         * 7 RejectedExecutionHandler handler:如果队列满了,按照我们指定的拒绝策略执行任务
         *
         *
         * 工作顺序:
         * 1) 线程池创建,准备好core数量的核心线程,准备接受任务
         *      1.1 core满了,就将再进来的任务放入阻塞队列中。空闲的core就会自己去阻塞队列获取任务执行
         *      1.2 阻塞队列满了,就直接开新线程执行,最大只能开到max指定的数量
         *      1.3 max满了就用RejectedExecutionHandler拒绝任务
         *      1.4 max都执行完成,有很多空闲,在指定的时间keepAliveTime以后释放max-core这些线程
         *
         *
         *       new LinkedBlockingDeque<>():默认是Integer的最大值。内存不够
         *
         *
         * 一个线程池 core 7,max 20,queue 50,100并发进来怎么分配;
         * 7个会立即得到执行,50个会进入队列,再开13个进行执行。剩下的30个就会使用拒绝策略。
         * 如果不想抛弃还要执行。CallerRunsPolicy;
         *
         * */
        System.out.println("main.......start......");
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
                200,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
//        Executors.newCachedThreadPool();core是0,所有都可回收
//        Executors.newFixedThreadPool();固定大小,core=max;都不可回收
//        Executors.newScheduledThreadPool()定时任务的线程池
//        Executors.newSingleThreadExecutor()单线程的线程池,后台从队列里面获取任务,挨个执行
        System.out.println("main.......end......");
    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
        }
    }

    public static class Runnable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程:" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果:" + i);
        }
    }

    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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值