
Vital techniques of Java 7 and polyglot programming The Benjamin J. Evans Martijn Verburg FOREWORD BY Dr. Heinz Kabutz MANNING 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 VITAL TECHNIQUES OF JAVA 7 AND POLYGLOT PROGRAMMING BENJAMIN J. EVANS MARTIJN VERBURG MANNING SHELTER ISLAND 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: [email protected] ©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 brief contents PART 1 DEVELOPING WITH JAVA 7 .............................................1 1 ■ Introducing Java 7 3 2 ■ New I/O 20 PART 2 VITAL TECHNIQUES .....................................................51 3 ■ Dependency Injection 53 4 ■ Modern concurrency 76 5 ■ Class files and bytecode 119 6 ■ Understanding performance tuning 150 PART 3 POLYGLOT 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 PART 4 CRAFTING 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 v contents foreword xvii preface xix acknowledgments xxi about this book xxiv about the authors xxix about the cover illustration xxx PART 1 DEVELOPING WITH JAVA 7..................................1 Introducing Java 7 3 1 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 New I/O 20 2 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 vii viii CONTENTS 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 PART 2 VITAL TECHNIQUES .........................................51 Dependency Injection 53 3 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 Modern concurrency 76 4 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 CONTENTS ix 4.2 Block-structured concurrency (pre-Java 5) 83 Synchronization and locks 83 ■ The state model for a thread 84 Fully synchronized objects 85 ■ Deadlocks 86 Why synchronized? 88 ■ The volatile keyword 89 Immutability 90 4.3 Building blocks for modern concurrent applications 91 Atomic classes—java.util.concurrent.atomic 92 Locks—java.util.concurrent.locks 93 ■ CountDownLatch 96 ConcurrentHashMap 97 ■ CopyOnWriteArrayList 99 Queues 102 4.4 Controlling execution 108 Modeling tasks 108 ■ ScheduledThreadPoolExecutor 110 4.5 The fork/join framework 111 A simple fork/join example 112 ■ ForkJoinTask and work stealing 114 ■ Parallelizing problems 115 4.6 The Java Memory Model (JMM) 116 4.7 Summary 118 Class files and bytecode 119 5 5.1 Classloading and class objects 120 Overview—loading and linking 120 ■ Verification 121 Class objects 122 ■ Classloaders 122 ■ Example—classloaders in Dependency Injection 124 5.2 Using method handles 125 MethodHandle 126 ■ MethodType 127 ■ Looking up method handles 127 ■ Example—reflection vs. proxies vs. MethodHandles 128 ■ Why choose MethodHandles? 131 5.3 Examining class files 132 Introducing javap 132 ■ Internal form for method signatures 132 ■ The constant pool 134 5.4 Bytecode 136 Example—disassembling a class 137 ■ The runtime environment 138 ■ Introduction to opcodes 140 Load and store opcodes 141 ■ Arithmetic opcodes 141 Execution control opcodes 142 ■ Invocation opcodes 143 Platform operation opcodes 143 ■ Shortcut opcode forms 144 Example—string concatenation 144 x CONTENTS 5.5 Invokedynamic 146 How invokedynamic works 146 ■ Example—disassembling an invokedynamic call 147 5.6 Summary 149 Understanding performance tuning 150 6 6.1 Performance terminology—some basic definitions 152 Latency 152 ■ Throughput 152 ■ Utilization 153 Efficiency 153 ■ Capacity 153 ■ Scalability 154 Degradation 154 6.2 A pragmatic approach to performance analysis 154 Know what you’re measuring 155 ■ Know how to take measurements 156 ■ Know what your performance goals are 157 ■ Know when to stop optimizing 157 Know the cost of higher performance 158 ■ Know the danger of premature optimization 158 6.3 What went wrong? Why we have to care 159 Moore’s Law—historic and future performance trends 160 Understanding the memory latency hierarchy 161 ■ Why is Java performance tuning hard? 162 6.4 A question of time—from the hardware up 163 Hardware clocks 163 ■ The trouble with nanoTime() 164 The role of time in performance tuning 166 ■ A case study— understanding cache misses 167 6.5 Garbage collection 169 Basics 170 ■ Mark and sweep 170 ■ jmap 172 Useful JVM parameters 176 ■ Reading the GC logs 177 Visualizing memory usage with VisualVM 178 Escape analysis 181 ■ Concurrent Mark-Sweep 182 G1—Java’s new collector 183 6.6 JIT compilation with HotSpot 184 Introduction to HotSpot 186 ■ Inlining methods 187 Dynamic compilation and monomorphic calls 188 Reading the compilation logs 188 6.7 Summary 190 CONTENTS xi PART 3 POLYGLOT PROGRAMMING ON THE JVM ........191 Alternative JVM languages 193 7 7.1 Java too clumsy? Them’s fighting words! 194 The reconciliation system 194 ■ Conceptual basics of functional programming 196 ■ Map and filter idioms 197 7.2 Language zoology 198 Interpreted vs. compiled languages 199 ■ Dynamic vs. static typing 199 ■ Imperative vs. functional languages 200 Reimplementation vs. original 201 7.3 Polyglot programming on the JVM 202 Why use a non-Java language? 203 Up-and-coming languages 204 7.4 How to choose a non-Java language for your project 205 Is the project area low-risk? 205 ■ Does the language interoperate well with Java? 206 ■ Is there good tooling and test support for the language? 207 ■ How hard is the language to learn? 207 Are there lots of developers using this language? 208 7.5 How the JVM supports alternative languages 208 Runtime environments for non-Java languages 209 Compiler fictions 209 7.6 Summary 211 Groovy: Java’s dynamic friend 213 8 8.1 Getting started with Groovy 215 Compiling and running 216 ■ Groovy console 217 8.2 Groovy 101—syntax and semantics 217 Default imports 219 ■ Numeric handling 219 Variables, dynamic versus static types, and scoping 220 Syntax for lists and maps 222 8.3 Differences from Java—traps for new players 223 Optional semicolons and return statements 224 Optional parentheses
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages496 Page
-
File Size-