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

Synchronizing tasks in a common point


The Java concurrency API provides a synchronizing utility that allows the synchronization of two or more threads in a determined point. It's the CyclicBarrier class. This class is similar to the CountDownLatch class explained in the Waiting for multiple concurrent events recipe in this chapter, but presents some differences that make them a more powerful class.

The CyclicBarrier class is initialized with an integer number, which is the number of threads that will be synchronized in a determined point. When one of those threads arrives to the determined point, it calls the await() method to wait for the other threads. When the thread calls that method, the CyclicBarrier class blocks the thread that is sleeping until the other threads arrive. When the last thread calls the await() method of the CyclicBarrier class, it wakes up all the threads that were waiting and continues with its job.

One interesting advantage of the CyclicBarrier class is that you can...