Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread-Safe issues  RSS feed

 
Andy Selador
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know most Swing components are not thread-safe, but does this mean I shouldn't even call getHeight() or isVisible() from anything but the Event Dispatch Thread (i.e. methods that do not modify the component)? The JavaDocs state (not quoted verbatim) that methods and components that are thread-safe are specifically stated as so, but get and is methods never mention either way.

Thanks,

Andy
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't call them outside the EDT if you're doing something that "depends on that state" - there's no guarantee of ordering. So the EDT thread could be updating the height of a component, and your thread reads it at the same time - there's no guarantee that you're going to get the old height value or the updated height value.

If you're doing something like just polling and printing out the values this isn't much of an issue.

If it's something like "if the component is visible, then perform some steps that require the component to be visible" - then you can't assume that the result of the if(component.isVisible()) is completely accurate - another thread may call component.setVisible(false) while you're in the middle of calling isVisible() or during the processing afterward.
 
Andy Selador
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!