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

Monitoring a Lock interface


A Lock interface is one of the basic mechanisms provided by the Java concurrency API to get the synchronization of a block of code. It allows the definition of a critical section. A critical section is a block of code that accesses a shared resource and can't be executed by more than one thread at the same time. This mechanism is implemented by the Lock interface and the ReentrantLock class.

In this recipe, you will learn what information you can obtain about a Lock object and how to obtain that information.

Getting ready

The example of this recipe has been implemented using the Eclipse IDE. If you use Eclipse or other IDE such as NetBeans, open it and create a new Java project.

How to do it...

Follow these steps to implement the example:

  1. Create a class named MyLock that extends the ReentrantLock class.

    public class MyLock extends ReentrantLock {
  2. Implement the getOwnerName() method. This method returns the name of the thread that has the control of a lock (if any) using...