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

Creating a fixed-size thread executor


When you use basic ThreadPoolExecutor created with the newCachedThreadPool() method of the Executors class, you can have a problem with the number of threads the executor is running at a time. The executor creates a new thread for each task that receives, (if there is no pooled thread free) so, if you send a large number of tasks and they have long duration, you can overload the system and provoke a poor performance of your application.

If you want to avoid this problem, the Executors class provides a method to create a fixed-size thread executor. This executor has a maximum number of threads. If you send more tasks than the number of threads, the executor won't create additional threads and the remaining tasks will be blocked until the executor has a free thread. With this behavior, you guarantee that the executor won't yield a poor performance of your application.

In this recipe, you are going to learn how to create a fixed-size thread executor modifying...