Forums Register Login

Accessing components in a new Thread

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Ever since I found this suit I've felt strange new needs. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 821 times.
Similar Threads
Stoping multiple threads
Design help - Need help in "static" implementation with Swing application
Wait in Java
NX: concerning the event-dispatching thread
event-dispatching thread
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:17:40.