title: Combine Java Swing and Java3D: performance problems with concurrency
body: <p>I am combining Swing and Java3D together. Manipulations of swing components should be done using the Event Dispatcher Thread, while manipulations of Java3D components should be done in the BehaviourSchedulerThread.</p>
  
  <p>Java3D renders the scene and then executes all of the behavior associated to the scene.</p>
  
  <p>I have a MouseListener enabled on the Canvas3D. Events are posted into the AWT Event queue. I then want to modify the Java3D environment based on these events, so I use a special Behavior where I can post Runnable's to. This makes sure the Runnable's are executed during the Behavior cycle of Java3D (and do not modify anyting during the Render cycle).</p>
  
  <p>Suppose some operations in the Behavior want to modify the Swing model. I then have to post a new Runnable to the EDT.</p>
  
  <p>Is this the correct way to do it?</p>
  
  <p>Using this technique, I experience a lot of problems on a mouse listener. I update a point in my Java3D model in the behaviour, and I update the swing GUI at the same time.</p>
  
  <p><strong>Update</strong>:
  The problem can be more clearly defined as follows:</p>
  
  <p>I have a JButton "spin cube" which has an ActionListener. Once the ActionListener is fired, it pushes the AWTEvent into a Java3D Behavior. Once the Behavior fires, it modifies the Scene graph and then modifies the JButton actionListener and text to become "Stop spinning".</p>
  
  <ol>
  <li>Click on the JButton twice.</li>
  <li>The first AWTEvent gets dispatched to SpinActionListener. The cube starts spinning and the JButton actionListener is modified to <code>StopSpinningActionListener</code>.</li>
  <li>The second AWTEvent gets dispatched to StopSpinningActionListener. The cube stops spinning and the JButton actionListener is modified to <code>SpinActionListener</code>.</li>
  </ol>
  
  <p>What actually happens is the following:</p>
  
  <ol>
  <li>Click on a JButton twice. Both <code>AWTEvent</code>s get dispatched to <code>SpinActionListener</code>. This creates a Runnable to execute inside the J3D Behavior.</li>
  <li>The first AWTEvent starts a timer to spin the cube. It then posts a Runnable to the EDT to modify the button.</li>
  <li>The second AWTEvent starts a timer to spin the cube. The cube will now spin twice as fast. It then posts a Runnable to the EDT to modify the button.</li>
  </ol>
  
  <p>Obviously, I should not be depending on AWTEvent's getting processed sequentially. I cannot wait in the EDT for the behavior to fire, because any SwingUtilities.invokeAndWait() will then cause a deadlock. </p>