
470
|
Chapter 12, Miscellany
#93 Code Models That Don’t Block
HACK
Exposing the Threading
But what if a caller wants to manage threading? You might want to at least
expose the fact that a thread is still updating and have the rest of your code
be aware of that state.
Notice that the
NonBlockingURLDocument exposes a pair of extra methods—
isAlive( ) and getProgress( )—that aren’t required by Document or anything
else in
javax.swing.text. These are extra methods I tossed into the demo to
support the progress bar mentioned earlier in this hack.
The strategy here is to have an outside caller periodically check in on the
NonBlockingURLDocument, get its progress, and update a progress bar. Notice I
said periodically: this is a job for
javax.swing.Timer! Using the Swing timer,
you get regular callbacks, and your code is guaranteed to be on the event-
dispatch thread.
So, uncomment the
makeProgressBarUpdaterFor( ) call shown earlier, and
add this implementation of that method, along with a helper method:
Figure 12-8. Progressive self-update of a JTextArea’s document