Applet thread safety issues

I have been converting a number of applets from jdk 1.3 to 1.4.2. I have noticed many peculiarities, like intermittently focus is incorrect or gets lost, or several blinking carets start appearing, or even none. On getting a tip that there might be thread safety issues, I liberally sprinkled my code with println of

SwingUtilities.isEventDispatchThread() and

Thread.currentThread().getName().

I ran the compiled code under JBuilder as well as a signed applet in IE browser. This is what I discovered:

1. In the initial bringup of the form, the main thread returned isEventDispatchThread=false and thread name of main (JB) or class name (IE). ActionPerformed methods returned the same results. FocusGained and Lost returned isEventDispatchThread=true and thread name of AWT-EventQueue-1 or AWT-EventQueue-3. I noticed the threads were interfering with one another.

2. Once the form was up, all of the methods returned isEventDispatchThread=true and the thread name was AWT-EventQueue-1 or AWT-EventQueue-3. I noticed that AWT-EventQueue-1 threads interrupted AWT-EventQueue-3 threads.

The questions I have for you are:

1. On the initial bringup of the form, why do focus gain and lost use awt event queue threads but actionPerformed uses the main thread?

2. On the initial bringup, how can I force everything (main, actionPerformed, focus gain/lost) to run under the same thread?

3. What determines which awt event queue thread focus gain and lost will operate under since they are invoked asynchronously?

Anup

[1586 byte] By [anupm_12a] at [2007-10-2 1:24:15]
# 1
Look at what they do in the main() method. using invokeLater() will fix your problem. http://java.sun.com/docs/books/tutorial/uiswing/learn/example1.html
jvaudrya at 2007-7-15 18:45:55 > top of Java-index,Desktop,Core GUI APIs...
# 2

My applet has a mainline thread and another thread that the main spawns off. I did indeed start using invokeLater at the very start of both threads. However, the situation is the same. Events continue to be handled by 2 awt event threads, causing trouble. Does anyone know how to ensure only one awt event thread is used?

Anup

anupm_12a at 2007-7-15 18:45:55 > top of Java-index,Desktop,Core GUI APIs...