title: Why does a method call to this function slow my thread? (Java)
body: <p>I am making a Conway's Game of Life program using java swing, and running it in the BlueJ IDE. I have a separate thread which runs the rules for the game, using swing worker. I created a method to check how many selected squares are around a specific cell.</p>
  
  <p><strong>Whenever I run the program with the checker method</strong> (below)<strong>, the code takes an excessively long time to execute. However, when I don't call the method, the code runs very fast.</strong> </p>
  
  <p><strong>Two questions:</strong> <strong>1)</strong> Why does this happen? <strong>2)</strong> How can I run the method without slowing it down?</p>
  
  <p>Here is my code.</p>
  
  <pre><code>
  private SwingWorker&lt;ArrayList&lt;Integer&gt;, Object[]&gt; createRunnable() {
          swingWorker = new SwingWorker&lt;ArrayList&lt;Integer&gt;, Object&gt;() {
  
              @Override
              protected ArrayList&lt;Integer&gt; doInBackground() throws Exception{
                  ArrayList&lt;Integer&gt; aiden = new ArrayList&lt;Integer&gt;();
                  Object[] objects = new Object[2];
                  while(running)
                  {
                      for(int i = 0; i &lt; square;i++)
                      {
                          for(int j = 0; j&lt; square; j++)
                          {
  
                              aiden = new ArrayList&lt;Integer&gt;(Arrays.asList((w/square) * j,(h/square) * i,(w/square),(h/square),i,j));
  
                              publish(aiden);
  
  
                              try{
                              Thread.sleep((long)5);
                          }
                          catch(Exception e){
                             throw e;
                          }
  
  
                          }
                      }
  
  
  
  
  
                  }
                  return aiden;
              }
              @Override
              protected void process(List&lt;Object&gt; chunks) {
                  ArrayList&lt;Integer&gt; aiden = new ArrayList&lt;Integer&gt;();
                  aiden = (ArrayList&lt;Integer&gt;)chunks.get(0);
                  int j = aiden.get(5);
                  int i = aiden.get(4);
                  ArrayList&lt;Integer&gt; selec = new ArrayList&lt;Integer&gt;(Arrays.asList(aiden.get(0),aiden.get(1),aiden.get(2),aiden.get(3)));
                  System.out.println("Hi");
  
                  if(selected.contains(selec) || surround(selec,i,j) == 0)
                                 { 
                                  selected.remove(selec);
  
                                  repaint();
                              }
                  repaint();   
              }
  
          };
          return swingWorker;
      }
  
  
  </code></pre>
  
  <pre><code>
   public int surround(ArrayList&lt;Integer&gt; j, int x, int y)
      {
          int count = 0;
  
          for(int i = -1; i &lt;= 1; i++)
          {
           for(int k = -1; i &lt;= 1; k++)
           {
  
               if(selected.contains(Arrays.asList((w/square) * (x + k), (h/square) * (y+i), (w/square),(h/square))))
               {
                   count+=1;
                  }
              }
          }
  
  
          return count; 
  
      }
  
  
  </code></pre>