开发者社区> 问答> 正文

Java中在子线程中获取固定大小的连接池出现错误,如果解释?报错

"

问题1. 不太好描述,直接看代码吧

public class QueueConsumer extends EndPoint implements Runnable {

// 线程池
private ExecutorService executor = null;

public QueueConsumer(String endPointName, String hosts, String ports,
        String username, String password) throws Exception {
    super(endPointName, hosts, ports, username, password);
    // 初始化线程池
    executor = Executors.newCachedThreadPool();
}

@Override
public void run() {
    try {
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(endPointName, false, consumer);
        while (true) {

            // 信息到达时
            Delivery delivery = consumer.nextDelivery();
            
            // 初始化业务线程对象
            BusinessClient businessClient = new BusinessClient(channel, delivery);
            // 执行任务
            executor.execute(businessClient);
        }
    } catch (Exception e) {
        LogUtil.printException("消息队列异常", e, LogUtil.LOG_LEVEL_ERROR);
    } finally {
        try {
            close();
        } catch (Exception e) {
            LogUtil.printException("关闭消息队列异常", e, LogUtil.LOG_LEVEL_ERROR);
        }
    }
}

}

public class BusinessClient implements Runnable { ... 省略

// 主要代码
List<Callable<Object>> taskList = new ArrayList<Callable<Object>>();
for (BaseRequestData<BaseResponseData> queryData : queryDataLst) {
    QueryThread queryThread = new QueryThread(queryData);
    taskList.add(queryThread);
}

// 问题点:此处使用Executors.newFixedThreadPool(i)会抛出IllegalArgumentException
          但是使用Executors.newCachedThreadPool()则不会有问题.不是很理解,为什么此处不可以用newFixedThreadPool(i)?
          
ExecutorService executor = Executors.newCachedThreadPool();
List<Future<Object>> results = executor.invokeAll(taskList);
executor.shutdown();

... 省略

}


问题2. 我如果定义一个线程池工厂,将线程池定义为static final,每个需要并发的地方都使用工厂来获得线程池,

   那么问题来了,我这个线程池会自动扩展或者释放线程吗?
   
   

纯粹自学的Java, 对于一些基础知识不是很理解,望不吝赐教!谢谢

"

展开
收起
因为相信,所以看见。 2020-05-27 10:02:08 998 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    "

    1、Executors.newFixedThreadPool(nThreads ):IllegalArgumentException - if nThreads <= 0
    2、final指的是你的线程池地址不会变化,但是线程池里的线程没有限制。

    ######

    Executors.newFixedThreadPool(i) 此处的i取值范围不对吧,如果你出现IllegalArgumentException,说明这个值是小于0的,这是不允许的。

    Executors.newFixedThreadPool(i) 最终会走下如下逻辑。

    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }
    "
    2020-05-27 17:38:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
多IO线程优化版 立即下载