线程池 异步线程

package com.xxxx.crm;

import com.sun.org.apache.xpath.internal.res.XPATHErrorResources_sv;

import java.util.Scanner;
import java.util.concurrent.*;

public class ay {
    public static ExecutorService service= Executors.newFixedThreadPool(10);
    public static void main(String[] args){
//        Student student = new Student();
//        Scanner aj=new Scanner(System.in);
//        String jj=aj.next();
//        student.setName("hj");
//        System.out.println(student);
        /**
         * int corePoolSize:【5】, 核心线程数(一直存在除非销毁); 线程池,创建好后就准备就绪的线程数量,就等呆来接受异步任务的去执行
         *          5相当于5个  Thread thread=nw Thread();  thread.start();
         * int maximumPoolSize,最大线程数量  控制资源
         * long keepAliveTime, 存活时间 如果当前正在运行的数量大于核心数量
         *  释放空闲的线程(大于核心线程的线程maximumPoolSize),只要线程空闲大于指定的存活时间KeepAliveTime
         * TimeUnit unit,时间单位
         * BlockingQueue<Runnable> workQueue,阻塞队列 如果任务很多,就会将目前多的任务放在队列里面
         *          只有有线程空闲了就会取出新的任务执行
         * ThreadFactory threadFactory,线程的创建工厂
         * RejectedExecutionHandler handler 如果队列满了,安照我们指定的拒绝策略拒绝执行任务
         * 工作顺序:
         * 1.线程池创建好,准备好核心线程数量,准备接收任务
         * 1.1核心线程满了,会将再进来的任务放入阻塞队列中 workQueue。空闲的核心线程就会自己去阻塞获取新任务执行
         * 1.2.阻塞队列满了,就直接开启新线程执行,最大只能开到max指定的数量
         * 1.3max满了就用RejectedExecutionHandler拒绝任务
         * 1.4max都执行好了,Max-core数量空闲的线程会在keepAliveRime指定的时间后自动销毁,最终保持核心线程数量大小
         *      new LinkedBlockingDeque<>():默认是Integer的最大值。会导致内存不够
         */
        /**
         * 常见的4种线程池 1.Executors.newCachedThreadPool() 2.Executors.newFixedThreadPool()
         * Executors.newCachedThreadPool() core是0,所有都可回收
         * Executors.newFixedThreadPool()  固定大小 core=max 都不可回收
         * Executors.newScheduledThreadPool() 定时任务线程池
         */
        System.out.println("man.......start.......");
//        Thread1 thread1 = new Thread1();
//        thread1.start();
//        FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable1());
//        new Thread(integerFutureTask).start();
//        new Thread(()->System.out.println("hello")).start();
//        service.execute(new Runable01());
//        Executors.newCachedThreadPool() core是0,所有都可回收
//        Executors.newFixedThreadPool()  固定大小 core=max 都不可回收
//        Executors.newScheduledThreadPool() 定时任务线程池
//        Executors.newSingleThreadExecutor() 单线程的线程池 后台从队列获取任务 挨个执行
        ThreadPoolExecutor executor=new ThreadPoolExecutor(5,200,10,TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

        System.out.println("man.......stop.......");

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

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

            @Override
            public Integer call() throws Exception {

                    System.out.println("当前线程:"+Thread.currentThread().getId());
                    int i=60/2;
                    System.out.println("运行结果:"+i);
                    return i;
                }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值