Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 7
A typical learning cycle with programming consists of four stages: (1) Study new concept; (2) execute sample program; (3)
compare output to expectation; and (4) iterate until the two match. Note, though, that I previously said the output for FirstThreadingExample would look "something like" Listing 2. So, that means your output could be different from mine, line by line. What's that about?
In the simplest Java programs, there is a guarantee of order-of-execution: the first line in main() will be executed first, then the next, and so on, with appropriate tracing in and out of other methods. Thread weakens that guarantee.
Threading brings new power to Java programming; you can achieve results with threads that you couldn't do without them. But
that power comes at the cost of determinacy. In the simplest Java programs, there is a guarantee of order-of-execution: the first line in main() will be executed first, then the next, and so on, with appropriate tracing in and out of other methods. Thread weakens that guarantee. In a multithreaded program, "Line #17 from thread B" might appear on your screen before or after "Line #14 from thread A," and the order might differ on successive executions of the same program, even on the same computer.
Indeterminacy may be unfamiliar, but it needn't be disturbing. Order-of-execution within a thread remains predictable, and there are also advantages associated with indeterminacy. You might have experienced something similar when working with graphical user interfaces (GUIs). Event listeners in Swing or event handlers in HTML are examples.
While a full discussion of thread synchronization is outside the scope of this introduction, it's easy to explain the basics.
For instance, consider the mechanics of how HTML specifies ... onclick = "myFunction();" ... to determine the action that will happen after the user clicks. This familiar case of indeterminacy illustrates some of its
advantages. In this case, myFunction() isn't executed at a definite time with respect to other elements of source code, but in relation to the end-user's action. So indeterminacy isn't just a weakness in the system; it's also an enrichment of the model of execution, one that gives the programmer new opportunities to determine sequence and dependency.
You can learn from FirstThreadingExample by experimenting with it on your own. Try adding or removing ExampleThreads -- that is, constructor invocations like ... new ExampleThread(label, delay); -- and tinkering with the delays. The basic idea is that the program starts three separate Threads, which then run independently until completion. To make their execution more instructive, each one delays slightly between
the successive lines it writes to output; this gives the other threads a chance to write their output.
Note that Thread-based programming does not, in general, require handling an InterruptedException. The one shown in FirstThreadingExample has to do with sleep(), rather than being directly related to Thread. Most Thread-based source does not include a sleep(); the purpose of sleep() here is to model, in a simple way, the behavior of long-running methods found "in the wild."
Threads programming
Thread synchronization
Locks and monitors
Testing and debugging threads
Threads & alternatives