[concurrency-interest] Confused about Component.getTreeLock()

Thomas Hawtin tackline at tackline.plus.com
Tue Mar 6 16:23:40 EST 2007


Gregg Wonderly wrote:
> 
> Thomas Hawtin wrote:
>>
>> For Swing components. In fact other stuff, like JTextField via 
>> documents, can use invokeLater internally and therefore screw up. So 
>> now it's do everything on the EDT (with a few exceptions). It turns 
>> out that the explicit multithreading capabilities of the Swing text 
>> doesn't actually work or indeed make any sense.
> 
> In that case, the component (the JTextField) is realized, so based on 

No. Even without being realised, documents can post to the EDT in 
response to be mutated from a non-EDT thread. JTextField has listeners 
on the object, so is being updated both on and off the EDT, without 
having been realised.

> the old verbage, you should "know" to use the EDT.  There are a number 
> of things that use InvokeLater to multitask the GUI better.  
> JComponent.revalidate() is one such thing.  If you've ever used a 
> resizing JPanel inside of JScrollPane, you know that you have to be 
> careful to use InvokeLater for anything that you do after a 
> JPanel.setPreferredSize() (such as repaint or other things that use the 
> new size).  If you do them in the EDT, and that EDT leg has invoked 
> setPreferredSize(), then you won't see the size changes that 
> setPreferredSize() effected.

Sometimes you need invokeLater even on the EDT. Usually this is down to 
listeners misbehaving (unfortunately they are often designed to misbehave).

> At the end of the day, it's a tough balancing act.  You want to allow 
> the application and the EDT to proceed independently, yet to be 
> synchronized in how they interact with each other.

You want to keep a very clear separation between what is EDT and what is 
not.

Tom Hawtin



More information about the Concurrency-interest mailing list