Book Image

Java 9 Programming By Example

By : Peter Verhas
Book Image

Java 9 Programming By Example

By: Peter Verhas

Overview of this book

This book gets you started with essential software development easily and quickly, guiding you through Java’s different facets. By adopting this approach, you can bridge the gap between learning and doing immediately. You will learn the new features of Java 9 quickly and experience a simple and powerful approach to software development. You will be able to use the Java runtime tools, understand the Java environment, and create Java programs. We then cover more simple examples to build your foundation before diving to some complex data structure problems that will solidify your Java 9 skills. With a special focus on modularity and HTTP 2.0, this book will guide you to get employed as a top notch Java developer. By the end of the book, you will have a firm foundation to continue your journey towards becoming a professional Java developer.
Table of Contents (17 chapters)
Title Page
About the Author
About the Reviewer
Customer Feedback

Atomic classes

Atomic classes enclose primitive values into objects and provide atomic operations on them. We discussed race conditions and volatile variables. For example, if we have an int variable to be used as a counter and we want to assign a unique value to objects that we work with, we can increment the value and use the result as a unique ID. However, when multiple threads use the same code, we cannot be sure about the value we read after the increment. It may happen that another thread also incremented the value in the meantime. To avoid that, we will have to enclose the increment and the assignment of the incremented value to an object into a synchronized block. This can also be done using AtomicInteger.

If we have a variable of AtomicInteger, then calling incrementAndGet increments the value of int enclosed in the class and returns the incremented value. Why do it instead of using synchronized block? The first answer is that if the functionality is there in the JDK, then using...