ThreadPoolExecutor线程池的使用及原理

线程池原理:
ThreadPoolExecutor线程池的使用及原理_第1张图片

package com.cy.java.api.thread;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
//JUC

public class ThreadPoolTests {
     
    public static void main(String[] args) {
     
    
    	//核心线程,核心线程满了,放入队列,队列满了开启再新线程。
        int corePoolSize=2;
        
        //最大线程数(核心线程+新线程),最大线程数、队列数都满了,默认抛异常
        int maximumPoolSize=3;
        
        //线程池中空闲线程等待工作的超时时间
        int keepAliveTime=60;
        
        //时间颗粒度转换(例如keepAliveTime默认单位为毫秒,转换成秒)
        TimeUnit unit=TimeUnit.SECONDS;
        
        //任务队列,存储要执行的任务,最大队列数设为1
        BlockingQueue<Runnable> workQueue=new ArrayBlockingQueue<>(1);
        
        //定义执行器
        RejectedExecutionHandler handler=
        //new ThreadPoolExecutor.AbortPolicy();//不能执行任务时,抛出异常
          new ThreadPoolExecutor.CallerRunsPolicy();//由调用者去执行

        //创建线程的工厂
        ThreadFactory threadFactory=new ThreadFactory() {
     
        	//定义线程名
            private String prefix="db-service-thread-";
            //生成自增长数
            private AtomicInteger atomicInteger=new AtomicInteger(0);
            @Override
            public Thread newThread(Runnable task) {
     
                return new Thread(task,prefix+atomicInteger.incrementAndGet());
            }
        };

          
        //构建线程池对象
        ThreadPoolExecutor threadPool= new ThreadPoolExecutor(
                        corePoolSize,
                        maximumPoolSize,
                        keepAliveTime,
                        unit,
                        workQueue,
                        threadFactory,
                        handler);


//设置休眠时间测试允许结果。核心线程数2,最大线程树3,最大队列1,超出最大限线程数时,由调用者去执行。
        threadPool.execute(new Runnable() {
     //task
            @Override
            public void run() {
     
                String tName=Thread.currentThread().getName();
                System.out.println(tName+"->execute->task1");
                try{
     Thread.sleep(5000);}catch (Exception e){
     }
            }
        });
        threadPool.execute(new Runnable() {
     //task
            @Override
            public void run() {
     
                String tName=Thread.currentThread().getName();
                System.out.println(tName+"->execute->task2");
                try{
     Thread.sleep(5000);}catch (Exception e){
     }
            }
        });
        threadPool.execute(new Runnable() {
     //task
            @Override
            public void run() {
     
                String tName=Thread.currentThread().getName();
                System.out.println(tName+"->execute->task3");
            }
        });
        threadPool.execute(new Runnable() {
     //task
            @Override
            public void run() {
     
                String tName=Thread.currentThread().getName();
                System.out.println(tName+"->execute->task4");
                try{
     Thread.sleep(5000);}catch (Exception e){
     }
            }
        });
        threadPool.execute(new Runnable() {
     //task
            @Override
            public void run() {
     
                String tName=Thread.currentThread().getName();
                System.out.println(tName+"->execute->task5");
                try{
     Thread.sleep(5000);}catch (Exception e){
     }
            }
        });

    }
}

/*运行结果:
db-service-thread-1->execute->task1
db-service-thread-2->execute->task2
db-service-thread-3->execute->task4
main->execute->task5
db-service-thread-2->execute->task3
*/

线程溢出时出错:
ThreadPoolExecutor线程池的使用及原理_第2张图片

你可能感兴趣的:(Java,java,多线程,thread,并发编程,队列)