【问题标题】:Getting stackOverFlow error within a run method在运行方法中获取 stackOverFlow 错误
【发布时间】:2013-12-01 15:07:50
【问题描述】:

这是得到错误的方法

public int getMaxTimeBetweenCustomers()
    {
        return Integer.parseInt(maxTimeBetweenCustomers);
    }

这是调用上述^方法的run方法。如果我尝试在方法之外初始化变量它不起作用,所以我不确定该怎么做。

 private void doSomething() throws InterruptedException
        {
         maxCustomers = myController.getMaxCustomers();
           int myCounter=0;
            String message;
            while(myCounter < maxCustomers)
            {
               Customer customer= this.generateCustomer();
               myServiceQueue = myServiceQueueManager.determineShortestQueue();
               myServiceQueue.insertCustomer(customer);
                 myTime = System.currentTimeMillis();
                myController.controllSetImages();


                myCounter++;
                System.out.println("my counter " + myCounter);


                try
                {
                    Thread.sleep(this.generateTimeBetweenCustomers());
                }
                catch(InterruptedException e)
                {
                    message = e.getMessage();
                    System.err.println(message);
                }

            }
        }

上面的方法doSomething()在run方法中被调用

【问题讨论】:

  • 你的代码进入了无限循环!
  • getMaxTimeBetweenCustomers 不可能导致 SO 异常。
  • @SameerSawla:这不仅仅是一个无限循环——它是递归的。
  • 堆栈跟踪会很有用
  • doSomething 不调用 getMaxTimeBetweenCustomers

标签: java multithreading function methods stack-overflow


【解决方案1】:

您声明 getMaxTimeBetweenCustomers() 方法导致您的程序抛出 StackOverflowException,但如果此方法仅包含您显示的代码行,这是不可能的。正在发生其他事情。

可能还有其他原因是您正在进行递归。我不得不猜测,但也许doSomething() 是从它包含的一种方法中直接或间接调用的,下面列出的方法之一:

  • this.generateCustomer();
  • myController.getMaxCustomers();
  • myServiceQueueManager.determineShortestQueue();
  • myServiceQueue.insertCustomer(customer);
  • myController.controllSetImages();

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 1970-01-01
    相关资源
    最近更新 更多