Thông tin tài liệu
MANNING
Benjamin J. Evans
Martijn Verburg
FOREWORD BY Dr. Heinz Kabutz
Vital techniques of Java 7 and polyglot programming
The
RULES OF THE JAVA MEMORY MODEL
Example of Synchronizes-With
JMM has the following rules:
■
An unlock operation on a monitor Synchronizes-With later lock operations
■
A write to a volatile variable Synchronizes-With later reads of the variable
■
If an action A Synchronizes-With action B, then A Happens-Before B
■
If A comes before B in program order within a thread, then A Happens-Before B
Read more in chapter 4.
The Well-Grounded Java Developer
The Well-Grounded
Java Developer
V
ITAL
T
ECHNIQUES
OF
J
AVA
7
AND
P
OLYGLOT
P
ROGRAMMING
BENJAMIN J. EVANS
MARTIJN VERBURG
MANNING
S
HELTER
I
SLAND
For online information and ordering of this and other Manning books, please visit
www.manning.com. The publisher offers discounts on this book when ordered in quantity.
For more information, please contact
Special Sales Department
Manning Publications Co.
20 Baldwin Road
PO Box 261
Shelter Island, NY 11964
Email: orders@manning.com
©2013 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written
permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in the book, and Manning
Publications was aware of a trademark claim, the designations have been printed in initial caps
or all caps.
Recognizing the importance of preserving what has been written, it is Manning’s policy to have
the books we publish printed on acid-free paper, and we exert our best efforts to that end.
Recognizing also our responsibility to conserve the resources of our planet, Manning books
are printed on paper that is at least 15 percent recycled and processed without the use of
elemental chlorine.
Manning Publications Co. Development editors: Renae Gregoire, Karen G. Miller
20 Baldwin Road Copyeditor: Andy Carroll
PO Box 261 Proofreader: Elizabeth Martin
Shelter Island, NY 11964 Typesetter: Dennis Dalinnik
Cover designer: Marija Tudor
ISBN: 9781617290060
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 12
v
brief contents
P
ART
1 D
EVELOPING
WITH
J
AVA
7 1
1
■
Introducing Java 7 3
2
■
New I/O 20
P
ART
2 V
ITAL
TECHNIQUES
51
3
■
Dependency Injection 53
4
■
Modern concurrency 76
5
■
Class files and bytecode 119
6
■
Understanding performance tuning 150
P
ART
3 P
OLYGLOT
PROGRAMMING
ON
THE
JVM 191
7
■
Alternative JVM languages 193
8
■
Groovy: Java’s dynamic friend 213
9
■
Scala: powerful and concise 241
10
■
Clojure: safer programming 279
P
ART
4 C
RAFTING
THE
POLYGLOT
PROJECT
311
11
■
Test-driven development 313
12
■
Build and continuous integration 342
13
■
Rapid web development 380
14
■
Staying well-grounded 410
vii
contents
foreword xvii
preface xix
acknowledgments xxi
about this book xxiv
about the authors xxix
about the cover illustration xxx
P
ART
1 D
EVELOPING
WITH
J
AVA
7 1
1
Introducing Java 7 3
1.1 The language and the platform 4
1.2 Small is beautiful—Project Coin 5
1.3 The changes in Project Coin 9
Strings in switch 9
■
Enhanced syntax for
numeric literals 10
■
Improved exception handling 12
Try-with-resources (TWR) 13
■
Diamond syntax 16
Simplified varargs method invocation 17
1.4 Summary 19
2
New I/O 20
2.1 Java I/O—a history 22
Java 1.0 to 1.3 22
■
Java 1.4 and NIO 23
Introducing NIO.2 24
CONTENTS
viii
2.2 Path—a foundation of file-based I/O 24
Creating a Path 27
■
Retrieving information from a Path 27
Removing redundancies 28
■
Converting Paths 29
NIO.2 Path and Java’s existing File class 30
2.3 Dealing with directories and directory trees 30
Finding files in a directory 30
■
Walking the directory tree 31
2.4 Filesystem I/O with NIO.2 33
Creating and deleting files 34
■
Copying and moving files 35
File attributes 36
■
Reading and writing data quickly 40
File change notification 41
■
SeekableByteChannel 42
2.5 Asynchronous I/O operations 43
Future style 44
■
Callback style 46
2.6 Tidying up Socket-Channel functionality 47
NetworkChannel 48
■
MulticastChannel 49
2.7 Summary 50
P
ART
2 V
ITAL
TECHNIQUES
51
3
Dependency Injection 53
3.1 Inject some knowledge—understanding IoC and DI 54
Inversion of Control 54
■
Dependency Injection 55
Transitioning to DI 56
3.2 Standardized DI in Java 61
The @Inject annotation 62
■
The @Qualifier annotation 64
The @Named annotation 65
■
The @Scope annotation 65
The @Singleton annotation 66
■
The Provider<T> interface 66
3.3 Guice 3—the reference implementation for DI in Java 67
Getting started with Guice 68
■
Sailor’s knots—the various
bindings of Guice 70
■
Scoping your injected objects
with Guice 73
3.4 Summary 75
4
Modern concurrency 76
4.1 Concurrency theory—a primer 77
Explaining Java’s threading model 77
■
Design concepts 79
How and why do the forces conflict? 80
■
Sources of overhead 81
A transaction processor example 81
[...]... him to the London Java Community (LJC—London’s Java User Group) The following Saturday was the annual Open Conference organized by the LJC— and it was at that conference that Ben met one of the leaders of the LJC, Martijn Verburg By the end of the day—fueled by their mutual love of teaching, technical communities, and beer—they’d resolved to collaborate on the project and what would become The Well-Grounded. .. should all, in theory, work the same way The specs don’t make guarantees about how well different implementations will perform when handling the same task, but they can provide assurances about the correctness of the results There are a number of separate specs that govern the Java system the most important are the Java Language Specification (JLS) and the JVM Specification (VMSpec) In Java 7, this separation... so separate in Java 7, how do they come together to make the familiar Java system? The link between the language and platform is the shared definition of the class file format (the class files) A serious study of the class file definition will reward you, and it’s one of the ways a good Java programmer can start to become a great one In figure 1.1 you can see the full process by which Java code is produced... to the heart of a lot of the topics in this book Here are our definitions: ■ ■ The Java language The Java language is the statically typed, object-oriented language that we lightly lampooned in the “About This Book” section Hopefully, it’s already very familiar to you One very obvious point about the Java language is that it’s human-readable (or it should be!) The Java platform The platform is the. .. Oracle’s “Plan B” strategy for releases The other big shift in recent years has been the rise of non -Java languages on the JVM This has led to cross-fertilization between Java and other JVM languages and there’s now a large (and growing) number of projects running completely on the JVM that include Java as one of the languages that they use The emergence of the polyglot project, particularly involving... comes with the book as they read through the text The rest of this section deals with how you can approach the book if you are more of a standalonechapter style of reader The Well-Grounded Java Developer is split into four parts: ■ ■ ■ ■ Developing with Java 7 Vital techniques Polyglot programming on the JVM Crafting the polyglot project ABOUT THIS BOOK xxvi Part 1 contains two chapters on Java 7 The book... approach to evolving the language Add functionality and complexity in libraries Broad, open ecosystem These design decisions have kept innovation moving in the Java world the simple core has kept the barrier to joining the developer community low, and the broad ecosystem has made it easy for newcomers to find pre-existing components that fit their needs These traits have kept the Java platform and language... productivity—all of which punch above their weight This will set the stage for the larger topic in this part—a chapter on new I/O in Java The well-grounded Java developer needs to be aware of the latest language features available Java 7 comes with several new features that will make your life as a working developer much easier But it isn’t enough simply to understand the syntax of these new changes In order... generics) Each of these changes may seem small, but exploring the semantics behind the simple syntax changes also gives you extra insight into the split between Java the language and Java the platform 2 PART 1 Developing with Java 7 The second set of changes is the new I/O (NIO.2) API, which completely overhauls Java s filesystem support as well as providing new powerful asynchronous capabilities These changes... grasp 1.1 The language and the platform The critical concept we’re kicking off with is the distinction between the Java language and the Java platform Surprisingly, different authors sometimes give slightly different definitions of what constitutes the language and platform This can lead to a lack of clarity and some confusion about the differences between the two and about which provides the programming . opinion, these are the most viable languages at the moment. The authors describe
the differences between these languages, how they compare to Java, and their. then A Happens-Before B
Read more in chapter 4.
The Well-Grounded Java Developer
The Well-Grounded
Java Developer
V
ITAL
T
ECHNIQUES
OF
J
AVA
7
AND
P
OLYGLOT
Ngày đăng: 22/03/2014, 17:20
Xem thêm: The Well-Grounded Java Developer potx, The Well-Grounded Java Developer potx, 2 Path—a foundation of file-based I/O, 1 Inject some knowledge—understanding IoC and DI, 3 Guice 3—the reference implementation for DI in Java, 3 What went wrong? Why we have to care, 4 A question of time—from the hardware up, 1 Java too clumsy? Them’s fighting words!, 3 Differences from Java—traps for new players, 4 Scala’s object model—similar but different, 4 Jenkins—serving your CI needs, 7 A sample Compojure project—“Am I an Otter or Not?”