使用Threads时的StackOverflowError

时间:2011-07-15 22:00:09

标签: java user-interface

我正在开发一个带有用户界面的射弹运动模拟程序。我收到StackOverflow错误,似乎发生在我尝试为我的按钮和文本字段创建动作侦听器的地方。我理解stackoverflow错误与永不退出的递归有关,但我没有在我的代码中看到发生这种情况。

我决定在我的程序中使用线程,因为我减慢了程序的耗时活动。所以我有一个线程来处理模拟的所有计算。

代码很长,但这是它的结构。

 public class SimulationGUI implements ActionListener, Runnable
 {
      //Create object references for window, panels, text fields, buttons, and labels. 

      SimulationGUI() 
      {
           //Here I instantiate all my buttons, textfields, labels, 
           //and basically set up the user interface.

           createListeners(); //This calls a method that creates my action listeners
      }   

      public void actionPerformed(ActionEvent e)
       {
           Thread thread = new Thread(this);
           thread.start();  // this calls the method run()
      }

      public void run()
      {
           //Here, I get the text for the variables  
           //calculations are performed for the projectile motion
           //I also edit three top level text fields using .setText()
      } 

      public void createListeners()
      {
           //Add action listeners for each text field
           ActionListener angleList = new SimulationGUI(); 
           angleText.addActionListener(angleList); 

           ..... 
      } 

      public static void main(String[] args)
      {
           new SimulationGUI(); 
      }

 }

我猜我必须在新线程中创建并添加动作侦听器,但我不完全确定。 以下是我得到的错误:

 Exception in thread "main" java.lang.StackOverflowError
     at java.awt.Insets.<init>(Unknown Source)
     at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
     at sun.awt.windows.WToolkit.getScreenInsets(Unknown Source)
     at java.awt.Window.init(Unknown Source)
     at java.awt.Window.<init>(Unknown Source)
     at java.awt.Frame.<init>(Unknown Source)
     at javax.swing.JFrame.<init>(Unknown Source)
     at SimulationGUI.<init>(SimulationGUI.java:24)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.<init>(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.<init>(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.<init>(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.<init>(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.<init>(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.<init>(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
 .....

2 个答案:

答案 0 :(得分:2)

是的,你有一个无限循环好吧。这是导致问题的代码的一部分:

SimulationGUI() 
{
    createListeners();
}

public void createListeners()
{
    ActionListener angleList = new SimulationGUI();
    angleText.addActionListener(angleList);
}

看看有什么问题?这两种方法都无条件地相互调用,并且将在剩余的时间内(或者至少在堆栈已满之前)这样做。证据在堆栈跟踪中:

at SimulationGUI.<init>(SimulationGUI.java:24)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)
at SimulationGUI.<init>(SimulationGUI.java:124)
at SimulationGUI.createListeners(SimulationGUI.java:211)

看看这些方法如何回归,第四次相互呼叫?你可能想要更像这样的东西:

SimulationGUI() 
{
    createListeners();
}

public void createListeners()
{
    angleText.addActionListener(this);
}

答案 1 :(得分:1)

 
Exception in thread "main" java.lang.StackOverflowError
     at java.awt.Insets.(Unknown Source)
     at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
     at sun.awt.windows.WToolkit.getScreenInsets(Unknown Source)
     at java.awt.Window.init(Unknown Source)
     at java.awt.Window.(Unknown Source)
     at java.awt.Frame.(Unknown Source)
     at javax.swing.JFrame.(Unknown Source)
     at SimulationGUI.(SimulationGUI.java:24)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
     at SimulationGUI.(SimulationGUI.java:124)
     at SimulationGUI.createListeners(SimulationGUI.java:211)
 

从查看代码和堆栈转储,我看到的直接问题是: 每次调用SimulationGUI构造函数时,都会调用createListeners,它会创建另一个调用createListeners的SimulationGUI,它会创建另一个SimulationGUI ...等。除此之外,每次调用createListeners时都会启动一个新线程。