Java Basics
Commentary - Threads
Threads execute independently and have their own call stacks, but all threads share heap and static memory. The importance of threads for GUI programs is that listeners run on the main GUI thread that process the AWT event queue, so the GUI is frozen while that listener is running -- nothing shows up as updated, and no user actions are possible. All GUI frameworks do this, not just Swing.
SwingWorker
. As long as a listener executes in a short time,
there is no problem, but large computations need to be run on their own threads
so the user feels the application is responsive.
One good way to do that is use the Java SwingWorker
class,
which is being rewritten and incorporated into the standard
language in Java 6.
Of course there are other related GUI issues -- preventing double clicks from
starting mulitple threads, implementing a cancel button, and showing progress
bars.
Multicore. Of course there are lots of reasons to use threads for non-GUI programming (eg, parallel execution of network operations). But there will soon be even more reason because CPUs are starting to be realeased in multicore versions, where the primarly speed increase is attainable only by multithreading, which JVMs are capable of.
But not covered here. Multithreading isn't described at all here, and probably won't be for a while.