
Think of
concurrent programming as
nothing more than
traffic management.
You may have been lucky in your programming work so far only to have to
think about
a single lane of traffic, relying on the road authorities
periodically to raise the
speed limit to
allow your vehicles to go
faster.
But as
hardware makers
lay
wider
freeways with multiple lanes, and as your
traffic
gets heavier, you
may need to start
worrying
about how to
use
the capacity better to get everyone where they need to be on time.
Perhaps building a thread gate, the equivalent of a traffic signal.
As
this article shows, and as applied to a prime number generator, a
thread gate let you manage the execution of tasks where there are
dependency on the completion of some of the tasks on others in your
program. Just as traffic at a four-way intersection is regulated by a
rule, albeit at intersections, a simple one: each two way direction
takes it in turns
to
avoid collisions.
Or using a
BlockingQueue,
already
part
of Java SE. A
BlockingQueue
is something akin to the kind of
smart
traffic signals that tracks waiting vehicles to decide when to
allow them to pass.
This
blog illustrates its use in bridging the gap between two intensive
tasks: parsing large amounts of data from heterogenous sources, and
persisting it.
Or, looking ahead to JDK7, a variant on the
BlockingQueue
called the
TransferQueue,
described
in detail here. Although not a perfect anaogy, its smarter kind of
traffic signal you have have seen
metering traffic
entering freeways, which takes into account both waiting vehicles
and the flow of traffic on the freeway.
So if you're tiring of life in the single lane, maybe its time to
experiment with the
Concurrency
Utilities in Java SE, and look further down the freeway to those
coming
in the future.
Update: Do check out
Brian's
talk about parallelism in Java from Devoxx here (How could
the Janitor forget, he
was in the audience).
Thanks
Patrick for the tip.