• Post Reply Bookmark Topic Watch Topic
  • New Topic

What EXACTLY should be run in the EDT?  RSS feed

 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am asking this, because even though I have been using Swing for some time now, I am a bit confused about it Better late than never, uh?

The question is: should I run every operation that relates to a component in the event dispatch thread? I know those methods that modify something on the component have to be, and this is documented everywhere, but what about getters? If Swing components are not thread safe, shouldn't they run in the EDT as well? If getters and setters are not synchronized, isn't it possible that a setter hits in from the EDT, in the middle of a get operation on another thread? And I am not even talking about get/setSelectionStart and get/setSelectionEnd in JTextComponent, which are separate methods, so even synchronization wouldn't help.

The more I think about it, the more it seems to me that getters have to be run in EDT, too. But why isn't this explained anywhere?
 
Paul Clapham
Sheriff
Posts: 22505
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes, you are absolutely correct. But it is explained somewhere. The rule, as it says there and as you have discovered for yourself, is
Once a Swing component has been realized, all code that might affect or depend on the state of that component should be executed in the event-dispatching thread.

[ March 21, 2007: Message edited by: Paul Clapham ]
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are absolutely right. Thanks for the confirmation

The problem is, that in the 99.9% of all resources (web pages, books even), only modification is mentioned. Even in the Java API, JTextComponent.setText is mentioned to be thread-safe, but getText is not. It would be nice to see clearer documentation at least in the API.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!