Java Reference
In-Depth Information
When the threads do not yield, each thread gets large chunks of time,
usually enough to finish all the prints without any other thread getting
any cycles. For example, suppose the program is run with
doYield
set to
false
in the following way:
Babble false 2 Did DidNot
The output may well look like this:
Did
Did
DidNot
DidNot
If each thread yields after each
println
, other printing threads will have
a chance to run. Suppose we set
doYield
to
true
with an invocation such
as this:
Babble true 2 Did DidNot
The yields give the other threads a chance to run, and the other threads
will yield in turn, producing an output more like this:
Did
DidNot
DidNot
Did
The output shown is only approximateperhaps you expected the words
to alternate? A different thread implementation could give different res-
ults, or the same implementation might give different results on dif-