【问题标题】:IllegalArgumentException from Executors.newFixedThreadPool() [closed]来自 Executors.newFixedThreadPool() 的 IllegalArgumentException [关闭]
【发布时间】:2017-06-27 05:27:14
【问题描述】:

线程“main”中的异常 java.lang.IllegalArgumentException
在 java.util.concurrent.ThreadPoolExecutor.(未知来源)
在 java.util.concurrent.ThreadPoolExecutor.(未知来源)
在 java.util.concurrent.Executors.newFixedThreadPool(未知来源)
在 alerts.email.VinrEmailNotification.main(VinrEmailNotification.java:50)

这是我的例外。我该如何解决?这是我的代码:

package alerts.email;  

import java.io.FileInputStream;  
import java.util.Properties; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors;  
import org.apache.log4j.Logger; 
import org.apache.log4j.PropertyConfigurator;  
import alerts.utils.Constants;  

/** The main entry point for the application which  
 *  creates a pool of threads for probing the  
 *  MessagesInTable, messages_sent_table and the  
 *  retrytable and uses the Java 5 Executor service  
 *  to run all the threads in the pool as parallel   
 *  tasks  
 *  
 *  @author Sunil Tuppale  
 *  @date July-19-2010  
 *  @version 1.0  
 */ 

public class VinrEmailNotification {     

    static final Logger logger = Logger.getLogger(VinrEmailNotification.class);      
    public static void main(String[] args) {                  
        Properties logProperties = null;         
        try {             //settings for logging             
            String fileName = System.getenv("LOG_PROPERTIES_FILE");
            if (fileName == null)
                fileName="vinralerts.properties";
            logProperties = new Properties(System.getProperties());
            logProperties.load(new FileInputStream(fileName));
            PropertyConfigurator.configure(logProperties);
            logger.debug("Logging initialized in VinrEmailNotification class ");          
        } catch(Exception e) {
            e.printStackTrace();
        }           
        /*          
         * create a thread pool with four threads
         */         
        int THREAD_POOL_SIZE = ConnectionPoolProvider.getInstance().getThreadPoolSize();
        ExecutorService messagesInTableTPExecSvc = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
        //ExecutorService messagesSentTableTPExecSvc = Executors.newFixedThreadPool(Constants.THREAD_POOL_SIZE);
        //ExecutorService retryTableTPExecSvc = Executors.newFixedThreadPool(Constants.THREAD_POOL_SIZE);
        /*          
         * place four tasks in the work queue for the thread pool         */
        for( int i = 0; i < THREAD_POOL_SIZE; i++ ) {
            messagesInTableTPExecSvc.execute(new MessagesInTableProbe(i));
            //messagesSentTableTPExecSvc.execute(new MessagesSentTableProbe(i));
            //retryTableTPExecSvc.execute(new RetryTableProbe(i));
        }
        /*
         * prevent other tasks from being added to the queue
         */         
        messagesInTableTPExecSvc.shutdown();
        //messagesSentTableTPExecSvc.shutdown()
        //retryTableTPExecSvc.shutdown(); 
        //ConnectionPoolProvider.getInstance().getDataSource().release();
    } 
}

【问题讨论】:

  • 格式化怎么样?!
  • 请务必在发布代码之前对其进行格式化。现在无法阅读。
  • @SalmaanC 没有。这不是编译器错误。
  • 您应该将 JVM 的代码加载到您的 IDE 中,这样您就可以查看实际异常发生的位置并自行解决。

标签: java multithreading


【解决方案1】:

表示THREAD_POOL_SIZE为0或负数。

来自Executors.newFixedThreadPool(int)的文档:

投掷:

IllegalArgumentException - if nThreads &lt;= 0

解决方案显然是确保ConnectionPoolProvider.getInstance().getThreadPoolSize() 返回一个正数(至少为1)。你是怎么做到的,我相信你最清楚,因为它是你自己的代码。或者您可以使用默认值(例如 1),以防提供商未提供有效值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多