JupiterOnlineMediahttp://www.DevX.com - The know-how behind application development.http://www.EarthWeb.comhttp://www.Internet.com - The Internet & IT Network.
Parallelaware

« Concurrency at SD West: James Reinders | Main | Joe Duffy on Concurrent Code Inspections »

March 11, 2008

Concurrency at SD West: Brian Goetz

At the recent SD West conference in Santa Clara, CA, concurrency was just barely on the agenda -- but if you were a programmer already on the path to parallelism, there were a few tips ripe for the picking.

Brian Goetz has been writing about concurrency for a while: Witness his Jolt-award-winning Java Concurrency in Action (Addison Wesley, 2006). Though he offered plenty of detail for Java junkies at SD West, I found myself grateful for his succinct and memorable metaphor for Amdahl's law:

"Harvesting crops can be sped up with more workers, but additional workers will not make them grow any faster. If you have 50% serialization in your program, the best speedup you'll get via concurrency is a factor of two, no matter how many processors you have," he said at his conference talk on Java concurrency.

"The primary source of serialization in programs is the exclusive resource lock. The longer locks are held, the worse it gets.
Even when tasks consist only of thread-local computation, there is still serialization inherent in task dispatching," Goetz explained. "Accessing the task queue and the results container invariably involves serialization."

One answer to this problem is to replace those data structures with ones that impose less serialization. For instance, the synchronous queue has been replaced in Java 6 by a better implementation for concurrency.

The other issue is how to hold locks for less time. Goetz suggested moving thread-local computation out of synchronization blocks, "but don’t make them so small as to split atomic operations." Another trick is to replace synchronization counters with AtomicInteger.

Finally, lock splitting can reduce contention: "You often write one very large lock, and sometimes that big fat lock on your hash map is the bottleneck. You can improve things by replacing with finer granularity locks; replace a synchronized Map with ConcurrentHashMap, which has separate locks for each access."

Posted by Alexandra Weber Morales on March 11, 2008 1:13 PM

Comments!!