![Page 1: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/1.jpg)
What’s New in Java 8Maurice Naftalin
![Page 2: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/2.jpg)
Developer, designer, architect, teacher, learner, writer
Maurice Naftalin
Co-authorCurrent Projects
www.lambdafaq.org
![Page 3: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/3.jpg)
What’s New in Java 8
- The Big Picture
- Date and Time API
- Type Annotations
- ... , ... ,
![Page 4: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/4.jpg)
Always Later Than You Think
The P6 Architecture:Background Information
for Developers
©1995, Intel Corporation
![Page 5: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/5.jpg)
Was Java Really Asleep?The library and VM developers certainly weren’t asleep! Java 5 introduced
- JSR 133 — fixing the Java Memory Model
- java.util.concurrent (new locks, blocking queues, atomic variables, non-blocking algorithms)
So what’s the problem?
- Without adequate synchronization, the Java Memory Model allows
- race conditions
- data visibility problems
- early writes, word tearing, ...
But why would anyone get synchronization wrong? :)
![Page 6: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/6.jpg)
Programming Used to be Really Hard
Varian 620/i
Writing machine code on the bare metal, there’s a lot to remember
![Page 7: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/7.jpg)
The Progress of Programming
There’s been a lot of progress:- Assemblers let us forget opcodes- Linkers let us forget absolute data location- Compilers let us forget register allocation and stack management- Virtual memory let us forget about paging- Garbage collectors let us forget memory management
Progress is being allowed to forget things!
How can we forget about parallelism?
![Page 8: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/8.jpg)
Why Can’t We Forget About Parallelism?
int sum = 0;for (int i = 0 ; i < a.length ; i++) {
sum += a[i];}
Because we keep writing code like this:
![Page 9: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/9.jpg)
Mutable State Makes Parallelism Hard
+=+=+=+=+=
sum
a
+=+=+=+=+=...
![Page 10: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/10.jpg)
Recursive Decomposition is “Easier”
∑∑∑ +
∑ ∑+
![Page 11: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/11.jpg)
Let the Library Writers do it!
Collections developers know the recursive structure of their data
But right now they can’t use that knowledge:
int sum = 0;for (Iterator<Integer> itr = myList.iterator() ; itr.hasNext() ; ) {
sum += itr.next();}
The problem is external iteration.
![Page 12: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/12.jpg)
Internal IterationBasic idea:
Let the collection choose its iteration strategy (parallel, serial, out-of-order, etc)
Instead of for we write forEach (examples)
Key is abstraction over behaviours
![Page 13: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/13.jpg)
Provide the basic operation, collection uses it to implement recursive decomposition
We just need to say “add two elements together” (<show, without receiver>)
Associative!
Summing an Integer List
+
{ {{+ +
{+
![Page 14: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/14.jpg)
Who are these guys?And why aren’t they Russian?
![Page 15: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/15.jpg)
History
java.util.Date, java.util.Calendar
- strong candidates for the all-time worst platform library design
Joda Time
- quality date and time library
JSR-310
- builds on experience of Joda Time
![Page 16: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/16.jpg)
Goals
- Comprehensive model for date and time
- Supporting commonly used global calendars
- Immutable, so as to work well with lambdas/functional
- Type-safe
![Page 17: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/17.jpg)
Design Principles
Immutable
- thread-safe, allows caching
Fluent
- easy to read, like a DSL
-
Extensible
LocalDate.of(2010, Month.DECEMBER, 3). withYear(2011).with(Month.May);
![Page 18: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/18.jpg)
Two ways of 'counting' time
- Continuous, designed for machines
• Single incrementing number
• java.time.Instant — nanos from 1970-01-01T00:00:00Z
Instant start = Instant.ofEpochMilli(123450L);Instant end = Instant.now();assert start.isBefore(end);assert end.isAfter(start);
![Page 19: What’s New in Java 8 · Was Java Really Asleep? The library and VM developers certainly weren’t asleep! Java 5 introduced-JSR 133 — fixing the Java Memory Model-java.util.concurrent](https://reader033.vdocuments.us/reader033/viewer/2022042317/5f06655a7e708231d417c8ac/html5/thumbnails/19.jpg)
Two ways of 'counting' time
- Field-based, designed for humans
• Year, month, day, hour, minute, second
• LocalDate, LocalDateTime, ZonedDateTime, Period, Duration, ...
Duration duration = Duration.ofSeconds(12);Duration bigger = duration.multipliedBy(4);Duration biggest = bigger.plus(duration);
Instant later = start.plus(duration);Instant earlier = start.minus(duration);