java Executor 线程池

java Executor 线程池

设置线程池的属性
* 参数1:corePoolSize:核心池大小
* 参数2:maximumPoolSize:最大线程池上限个数
* 参数3:keepAliveTime:保存最长时间,任务执行完之后,要裁员的延时
* 参数4:unit:时间单位
* 参数5:workQueue:用于存储任务的工作队列(即将被执行的任务)(BlockingQueue)
* 参数6:ThreadFactory: 线程工厂, 用来创建线程的

package cn.Douzi.Thread_Pool;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPoolTest02 {

    //网络访问框架,都要用线程池
    private static Executor executors = Executors.newCachedThreadPool(); //缓存线程池
    private static Executor executor2 = Executors.newFixedThreadPool(5); //固定线程个数的线程
    private static Executor executor3 = Executors.newScheduledThreadPool(5); //计划任务线程池
    private static Executor executor4 = Executors.newSingleThreadExecutor(); //单个线程的线程池


    public static void main(String[] args) {

//        BlockingQueue<E> //单端队列
//        BlockingDQueue   //双端队列
        LinkedBlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>(100); //该容器的最大上限
        //创建一个线程工厂
        ThreadFactory threadFactlory = new ThreadFactory() {

            //线程安全的int的包装类
            AtomicInteger atomicInteger = new AtomicInteger(0);
            @Override
            public Thread newThread(Runnable r) {
                //创建一个线程,然后把r赋值给该线程
                Thread thread = new Thread(r);
                thread.setName("MyThread=" + atomicInteger.getAndIncrement());

                return thread;
            }
        };

        /*
         * 参数1:corePoolSize:核心池大小
         * 参数2:maximumPoolSize:最大线程池上限个数
         * 参数3:keepAliveTime:保存最长时间,任务执行完之后,要裁员的延时
         * 参数4:unit:时间单位
         * 参数5:workQueue:用于存储任务的工作队列(即将被执行的任务)(BlockingQueue)
         * 参数6:ThreadFactory: 线程工厂, 用来创建线程的
         *
         */
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5, 10, 1, TimeUnit.SECONDS, blockingQueue, threadFactlory);

        /**
         * 线程不是越多越好,Google工程给了一个推荐值:线程的个数=CPU核心数+1=5
         */

        //用自己打造的线程池
        for (int i = 0; i < 110; i++) {
            poolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        method();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    //同时最多只运行5个进程过来
    public static  void method() throws InterruptedException {
        System.out.println("ThreadName= " + Thread.currentThread().getName()+"进来了");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("ThreadName= "+Thread.currentThread().getName()+"出去了");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用配置类来配置Executor线程池。其中,常用的实现类是`ExecutorService`和`ThreadPoolTaskExecutor`。 首先,需要创建一个配置类,可以命名为`ThreadPoolConfig`或者其他你喜欢的名称。在这个类中,你需要使用`@Configuration`注解来标识它是一个配置类,并且使用`@EnableAsync`注解来启用异步执行。 接下来,你需要定义一个`ExecutorService`或`ThreadPoolTaskExecutor` Bean,并使用`@Bean`注解将其标识为一个Bean。你可以根据项目的需求来选择使用哪个实现类。 如果选择使用`ExecutorService`,可以按照以下方式配置: ```java @Configuration @EnableAsync public class ThreadPoolConfig { @Bean public ExecutorService executorService() { return Executors.newFixedThreadPool(10); // 配置线程池的大小 } } ``` 如果选择使用`ThreadPoolTaskExecutor`,可以按照以下方式配置: ```java @Configuration @EnableAsync public class ThreadPoolConfig { @Bean public ThreadPoolTaskExecutor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); // 设置核心线程数 executor.setMaxPoolSize(20); // 设置最大线程数 executor.setQueueCapacity(30); // 设置队列容量 executor.setThreadNamePrefix("my-executor-"); // 设置线程名称前缀 executor.initialize(); // 初始化 return executor; } } ``` 在上述配置中,你可以根据实际需求来设置线程池的大小、队列容量等参数。通过这种方式,你就可以在应用程序中注入`ExecutorService`或`ThreadPoolTaskExecutor`,并使用它来执行异步任务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值