线程池

	经常创建,启动,销毁一个线程都是非常耗时间的。使用线程池进行管理服务线程,提高程序效率。
	1.重复利用
	2.提高响应速度
	3.管理线程---线程池进行创建和分配。--队列
	java中使用线程核心走ThreadPoolExecutor

线程池的最上层接口是Executor,这个接口定义了一个核心方法execute(Runnabel command),这个方法最后被ThreadPoolExecutor类实现,这个方法是用来传入任务的。而且ThreadPoolExecutor是线程池的核心类,此类的构造方法如下:

public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue);
 
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory);
 
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue,RejectedExecutionHandler handler);
 
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
        BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler);

构造方法的参数及意义:

corePoolSize:核心线程池的大小,如果核心线程池有空闲位置,这是新的任务就会被核心线程池新建一个线程执行,执行完毕后不会销毁线程,线程会进入缓存队列等待再次被运行。

maximunPoolSize:线程池能创建最大的线程数量。如果核心线程池和缓存队列都已经满了,新的任务进来就会创建新的线程来执行。但是数量不能超过maximunPoolSize,否侧会采取拒绝接受任务策略。

keepAliveTime:非核心线程能够空闲的最长时间,超过时间,线程终止。这个参数默认只有在线程数量超过核心线程池大小时才会起作用。只要线程数量不超过核心线程大小,就不会起作用。

unit:时间单位,和keepAliveTime配合使用。
workQueue:缓存队列,用来存放等待被执行的任务。
threadFactory:线程工厂,用来创建线程
handler:拒绝处理策略,线程数量大于最大线程数就会采用拒绝处理策略

提交一个任务到线程池中,线程池的处理流程如下:
1.判断线程池里的核心线程是否都在执行任务,如果不是(核心线程空闲或者还有核心线程没有被创建)则创建一个新的工作线程来执行任务。如果核心线程都在执行任务,则进入下个流程。
2.线程池判断工作队列是否已满,如果工作队列没有满,则将新提交的任务存储在这个工作队列里。如果工作队列满了,则进入下个流程。
3.判断线程池里的线程是否都处于工作状态,如果没有,则创建一个新的工作线程来执行任务。如果已经满了,则交给饱和策略来处理这个任务。

在这里插入图片描述

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

/**
 * @author shkstart
 * 
 */
public class Test1 {
    public static void main(String[] args) {
        //1.可缓存的线程池,重复利用newCachedThreadPool
        ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
        for (int i = 0; i <20 ; i++) {
            int temp=i;
            newCachedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("threadName:"+Thread.currentThread().getName()
                            +",i:"+temp);
                }
            });
        }



        //2.可固定长度线程池newFixedThreadPool
//        ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(3);
//        for (int i = 0; i <10 ; i++) {
//            int temp=i;
//            newFixedThreadPool.execute(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println("threadName:"+Thread.currentThread().getName()
//                            +",i:"+temp);
//                }
//            });
//        }
//
//
//
//        //3.可定时线程池newScheduledThreadPool
//        ScheduledExecutorService newScheduledThreadPool = Executors.newScheduledThreadPool(3);
//        for (int i = 0; i <10 ; i++) {
//            int temp=i;
//            newScheduledThreadPool.schedule(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        Thread.sleep(500);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    System.out.println("threadName:"+Thread.currentThread().getName()
//                            +",i:"+temp);
//                }
//            },5, TimeUnit.SECONDS);
//        }
//        newScheduledThreadPool.shutdown();
//
//
//        public void test4(){
//            //4.单线程newSingleThreadScheduledExecutor
//            ScheduledExecutorService newSingleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
//            for (int i = 0; i <10 ; i++) {
//                int temp=i;
//                newSingleThreadScheduledExecutor.execute(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            Thread.sleep(500);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        System.out.println("threadName:"+Thread.currentThread().getName()
//                                +",i:"+temp);
//                    }
//                });
//            }
//            newSingleThreadScheduledExecutor.shutdown();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值