O'Reilly logo

Java 7 Concurrency Cookbook by Javier Fernández González

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using atomic arrays

When you implement a concurrent application that has one or more objects shared by several threads, you have to protect the access to their attributes using a synchronization mechanism as locks or the synchronized keyword to avoid data inconsistency errors.

These mechanisms have the following problems:

  • Deadlock: This situation occurs when a thread is blocked waiting for a lock that is locked by other threads and will never free it. This situation blocks the program, so it will never finish.
  • If only one thread is accessing the shared object, it has to execute the code necessary to get and release the lock.

To provide a better performance to this situation, the compare-and-swap operation was developed. This operation implements ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required