Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing components in a new Thread  RSS feed

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
I'm reading Art Of Java book and I have a question about Web Crawler example...
the application has a button called "Search".
This button is associated with an event listener, actionPerformed( ) method called a private method called actionSearch( ).
actionSearch( ) tries to verify that some fileds are not blank, if not, it will call a private method called search( ).
search( ) method's logic is under a new thread like :

The first lines of run( ) method are disabling text fields and the Search button.
The author says that he used a new Thread to release the Swing event thread.
Here, I have my question :
AFAIK, we use SwingUtilities.invokeLater( ) method when we want to update or access the visual components out of the swing events thread.
How the auther is disabling swing components in a new thread ?
Is it ok ?
Thanks.
 
Jason Kingsley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it's not ok to do it.

In most cases a Swing control isn't going to throw a nasty exception just because it's been accessed from a different thread but you run the enormous risk of unsynchronised access to it, the control may be in the middle of a repaint and boom, it's state gets changed.

By the sounds, a bad example in a book.

It would be interesting to know how much "example" code makes it into production systems. It's a scarey thought, and it's even worse to see terrible examples being foisted upon the public.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jason. The problem is that when Swing is used to teach something else, just enough Swing code is done to make the author's point. In this case, the code to crawl the web is the focus. They used a Swing app so that you weren't doing it all from the command line.

In thier defense however, I'd like to say that if they focused on getting the Swing code correct, they might never have finished the book.
 
Jason Kingsley
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oooo that's a can of worms... ; )

I would counter that some things in Swing might be a bit hard for starters, but I would hope that authors in a book would at least get the basics correct!
Controls and threading is something that's going to bite you in the behind in most systems.

There's nothing worse then examples in a book where a toolkit is used incorrectly at the very basic level. A lot of people read them (hopefully, if print medium isn't quite dead yet) and bang, they're in real world systems because that's how they thought it should be done.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!