Re: How to change JPanels?
On 16/02/11 18:27, Joshua Cranmer wrote:
On 02/16/2011 11:42 AM, Eric wrote:
On Feb 16, 9:26 am, Lew<no...@lewscanon.com> wrote:
To summarize. There is one EDT. Swing handles that threading
automatically.
You have to give it GUI commands to handle, otherwise they simply
cannot run
on the EDT. You are the programmer. You put the calls on the EDT,
using
'invokeLater()' and its sister. The EDT is a single thread and
there's only
one of it. If you run slow things on the EDT, you will have a slow
GUI. That
is bad. Run slow things off the GUI. Run GUI things on the GUI.
It's up to
you do to that.
--
Lew
Don't quote sigs.
From the Oracle docs:
public static boolean isEventDispatchThread()
Returns true if the current thread is an AWT event dispatching thread.
<http://download.oracle.com/javase/6/docs/api/java/awt/EventQueue.html#isDispatchThread%28%29>:
"Returns true if the calling thread is the current AWT EventQueue's
dispatch thread."
This is where you're confusing. You're saying there's one EDT.
Oracle says there's more. It does not say *the* EDT, it says *an*
EDT.
I see that the method here clearly states *the*, not *an*. Actually, if
I recall some of the really, really dirty implementation details, it is
actually possible to have multiple event dispatch threads, but only at
most one will exist at any point in time (consider, e.g., what happens
when you throw an uncaught exception on the EDT). It is also possible
that having multiple applets in one page can cause things to get really
wonky.
and, maybe, it's possible with different classloaders. But this is mere
speculation.
In any "normal" Swing application there is only one EDT. All Swing
components must only be accessed by a single thread at any one time
(they are not thread safe, they are not synchronized). Since you don't
control the EDT, and cannot state with certainty that the EDT is not
accessing a component you want to change the only safe way to change
that component is to ask the EDT to do it for you. You might, if you
were so inclined, invent some complicated synchronization mechanism for
your own component classes and use that to allow your threads to access
your components safely whilst the EDT is running. But absent that
specialist synchronization the rule of only changing components on the
EDT should be rigidly adhered to. Failure to do so is to invite
unnecessary bugs into your code, bugs which are unpredictable,
unrepeatable and nigh on impossible to trace.
There was a time when Sun thought it was safe to create components on
the main thread. Only after those components were realized was it
essential to only access them via the EDT. But Sun were wrong, and they
discovered it the hard way, by strange unpredictable bugs. I've also
seen the same myself. It's rare, but it does happen, if you perform any
component activity, even just invoking a constructor, on a thread which
isn't the EDT.
To the OP:
Go there at your own peril, but please don't invite us to follow you. We
can attempt to pull you back from the brink, but once you've gone over
the edge you are on your own.
--
Nigel Wade