title: game loop - threads
body: <p>should a game loop in a java program always run in the event dispatch thread?
  Because my keylisteners are running in the AWT event thread and I want the key events to be processed in the same thread as the rest of the game to avoid thread based bugs.
  If I don't run the game loop in the event thread can I use an ArrayBlockingQueue to send events from the event handler to the game loop? Is there a better solution?
  thanks</p>
  
  <p>Maybe something like this?</p>
  
  <pre><code>ConcurrentLinkedQueue&lt;AWTEvent&gt; eventQueue = new ConcurrentLinkedQueue&lt;AWTEvent&gt;(); 
  
  JFrame frame = new JFrame() {
      @override
      protected void processEvent(AWTEvent e) {
          if(e instanceof InputEvent) eventQueue.add(e);
          else super.processEvent(e);
      }
  }
  
  //game loop
  while(true) {
      while(!eventQueue.isEmpty()) frame.super.processEvent(eventQueue.poll());
      ...
  }
  </code></pre>
  
  <p>edit: Thank you, I think I now know how to do this. I'm gonna do it like this</p>
  
  <pre><code>import java.util.concurrent.ConcurrentLinkedQueue;
  import java.awt.AWTEvent;
  import java.awt.Dimension;
  import java.awt.event.*;
  import javax.swing.*;
  
  public class QueueTest {
  
      static ConcurrentLinkedQueue&lt;AWTEvent&gt; eventQueue = new ConcurrentLinkedQueue&lt;AWTEvent&gt;(); 
  
      public static void main(String[] args) {
          final JPanel panel = new JPanel() {
              @Override
              protected void processEvent(AWTEvent e) {
                  eventQueue.add(e);
              }
              {
                  enableEvents(  AWTEvent.MOUSE_EVENT_MASK
                               | AWTEvent.MOUSE_MOTION_EVENT_MASK
                               | AWTEvent.KEY_EVENT_MASK);
              }
          };
          final JFrame frame = new JFrame();
          frame.addComponentListener(new ComponentAdapter() {
              @Override
              public void componentShown(ComponentEvent e) {
                  panel.requestFocusInWindow();
              }
          });
          frame.add(panel);
          panel.setPreferredSize(new Dimension(800,600));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setVisible(true);
  
          while(true) {
              while(!eventQueue.isEmpty()) System.out.println(eventQueue.poll()+"\n");
          }
      }
  }
  </code></pre>