Book Image

Java 7 Concurrency Cookbook

By : Javier Fernández González
Book Image

Java 7 Concurrency Cookbook

By: Javier Fernández González

Overview of this book

<p><undefined:p><undefined:undefined:p>Java remains the global standard for developing various applications and enterprise software, and the launch of Java 7 brings with it exciting new capabilities for concurrent programming by way of the concurrency utilities enhancement. This allows developers to make the most of their applications with parallel task performance. "Java 7 Concurrency Cookbook" covers all elements of the Java concurrency API, providing essential recipes for taking advantage of the exciting new capabilities.<undefined:undefined:br><undefined:undefined:br>On your computer, you can listen to music while you edit a Word document and read your emails, all at once! This is because your operating system allows the concurrency of tasks, much like the Java platform which offers various classes to execute concurrent tasks inside a Java program. "Java 7 Concurrency Cookbook" covers the most important features of the Java concurrency API, with special emphasis on the new capabilities of version 7. <undefined:undefined:br><undefined:undefined:br>With each version, Java increases the available functionality to facilitate development of concurrent programs. This book covers the most important and useful mechanisms included in version 7 of the Java concurrency API, so you will be able to use them directly in your applications.<undefined:undefined:br><undefined:undefined:br>"Java 7 Concurrency Cookbook" includes recipes to enable you to achieve everything from the basic management of threads and tasks, to the new Fork /Join framework, through synchronization mechanisms between tasks, different types of concurrent tasks that Java can execute, data structures that must be used in concurrent applications and the classes of the library that can be customized.<undefined:undefined:br><undefined:undefined:br>With the step-by-step examples in this book you&rsquo;ll be able to apply the most important and useful features of the Java 7 concurrency API.</undefined:undefined:br></undefined:undefined:br></undefined:undefined:br></undefined:undefined:br></undefined:undefined:br></undefined:undefined:br></undefined:undefined:br></undefined:undefined:br></undefined:undefined:p></undefined:p></p>
Table of Contents (15 chapters)
Java 7 Concurrency Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using atomic variables


Atomic variables were introduced in Java Version 5 to provide atomic operations on single variables. When you work with a normal variable, each operation that you implement in Java is transformed in several instructions that is understandable by the machine when you compile the program. For example, when you assign a value to a variable, you only use one instruction in Java, but when you compile this program, this instruction is transformed in various instructions in the JVM language. This fact can provide data inconsistency errors when you work with multiple threads that share a variable.

To avoid these problems, Java introduced the atomic variables. When a thread is doing an operation with an atomic variable, if other threads want to do an operation with the same variable, the implementation of the class includes a mechanism to check that the operation is done in one step. Basically, the operation gets the value of the variable, changes the value in a local variable...