Top Banner
Mohsen Zainalpour [email protected] What`s New in Java 8 JUG
75
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Mohsen [email protected]

What`s New in Java 8

JUG

Page 2: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

• 2012/04/26 M1• 2012/06/14 M2• 2012/08/02 M3• 2012/09/13 M4• 2012/11/29 M5• 2013/01/31 M6• 2013/06/13 M7 Feature Complete• 2013/09/05 M8 Developer Preview• 2014/01/23 M9 Final Release Candidate• 2014/03/18 GA General Availability

JDK 8Schedule and status

Page 3: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Major changes

Agenda

JUG

Why is Java still changing?

Type Annotations

Lambda Project

Date and Time API

Compact Profiles

1

2

3

4

5

Page 4: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Java 8 Is Revolutionary, Java Is Back

“Lambda is the single largest upgrade to the programming model. Ever. It's larger even than Generics. It's the first time since the beginning of Java that we've done a carefully coordinated co-evolution of the virtual machine, the language and the libraries, all together. Yet the result still feels like Java.  Mark Reinhold (Chief Architect of the Java Platform Group at Oracle)

Page 5: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Java 8 Is Revolutionary, Java Is Back

“Lambda is the single largest upgrade to the programming model. Ever. It's larger even than Generics. It's the first time since the beginning of Java that we've done a carefully coordinated co-evolution of the virtual machine, the language and the libraries, all together. Yet the result still feels like Java.  Mark Reinhold (Chief Architect of the Java Platform Group at Oracle)

Page 6: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

LambdaJSR 335

Page 7: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why is Java still changing?

Page 8: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why is Java still changing?

Page 9: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why is Java still changing?

Page 10: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why is Java still changing?

Page 11: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why is Java still changing?

Page 12: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why is Java still changing?

Page 13: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why is Java still changing?

Page 14: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why is Java still changing?

The changing computing background

Increasingly dealing with big data (terabytes and up) and wishing to exploit multicore computers or computing clusters effectively to process this.

And this means using:

parallel processing

big data multicore cloud computing

Page 15: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

The Progress of Programming

There`s been a lot of progress

&

Progress is being allowed to forget things!

Page 16: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

The Progress of Programming

There`s been a lot of progress:

- Assemblers let us forget opcodes

Page 17: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

The Progress of Programming

There`s been a lot of progress:

- Assemblers let us forget opcodes- Garbage collections let us forget memory management

#include <stdio.h> #include <stdlib.h> int main () {…

buffer = (char*) malloc (i+1); …

free (buffer);…}

Page 18: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

The Progress of Programming

So what about parallelism?

Page 19: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

Most of parallelism problems are doing bulk operations on collection

and we keep writing code like this:

int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; }

It is inherently serial

Page 20: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

If the processing of different elements is to proceed in parallel, it is the responsibility of the client code

Page 21: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

If the processing of different elements is to proceed in parallel, it is the responsibility of the client code

n1 n2 n3 n4 n5 n6 n7 n8 n9 … … … …

sum1 sum2 sum3 sum4

Page 22: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

If the processing of different elements is to proceed in parallel, it is the responsibility of the client code

n1 n2 n3 n4 n5 n6 n7 n8 n9 … … … …

class Sum implements Callable<Long> {private final long from;private final long to;Sum(long from, long to) {

this.from = from;this.to = to;

}public Long call() {

long acc = 0;for (long i = from; i <= to; i++) {

acc = acc + i;}return acc;}

}

sum1 sum2 sum3 sum4

Page 23: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

If the processing of different elements is to proceed in parallel, it is the responsibility of the client code

n1 n2 n3 n4 n5 n6 n7 n8 n9 … … … …

sum1 sum2 sum3 sum4

ExecutorService executor = Executors.newFixedThreadPool(2);List<Future<Long>> results = executor.invokeAll(asList(

new Sum(1, 250),new Sum(251, 500),new Sum(551, 750),new Sum(751, 1000)

));for (Future<Long> result : results) {

System.out.println(result.get());}

Page 24: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

Sequential

Parallel(assuming a quad core machine)

Time t

Thread 1

Thread 1

Thread 2

Thread 3

Thread 4

Done parallel

Done sequential

Page 25: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

Let the Library Writers do it!

Collection developers know the recursive structure of their data

But right now they can`t use that knowledge

Page 26: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

Let the Library Writers do it!

Collection 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 27: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

Internal Iteration

Instead of this:

int sum = 0;

for (int i=0;i<myList.size();i++){

int a=myList.get(i);sum+=a;

}

Page 28: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

Internal Iteration

We`re going to write this:

int [] sum = new int[1];

myList.forEach(…

);

Instead of this:

int sum = 0;

for (int i=0;i<myList.size();i++){

int a=myList.get(i);sum+=a;

}

Page 29: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

Internal Iteration

• Let the collection choose its iteration strategy- Parallel, serial, out-of-order, lazy, …

We`re going to write this:

int [] sum = new int[1];

myList.forEach(…

);

Instead of this:

int sum = 0;

for (int i=0;i<myList.size();i++){

int a=myList.get(i);sum+=a;

}

Page 30: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

Internal Iteration

• Let the collection choose its iteration strategy- Parallel, serial, out-of-order, lazy, …

We`re going to write this:

int [] sum = new int[1];

myList.forEach(a -> sum[0]+=a

);

Instead of this:

int sum = 0;

for (int i=0;i<myList.size();i++){

int a=myList.get(i);sum+=a;

}

Page 31: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Why Can`t We Forget About Parallelism?

a -> sum[0]+=a

Page 32: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Lambda Project – JSR 335

JSR 335 (Lambda Project) aims to support programming in a

multicore environment

by adding closures and related features to the Java language.

Page 33: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

What are Lambda Expressions (closures)?

A lambda expression :

Page 34: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

What are Lambda Expressions (closures)?

is an anonymous

method

A lambda expression :

Page 35: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

What are Lambda Expressions (closures)?

is an anonymous

method

having an argument

list

A lambda expression :

a ->

Page 36: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

What are Lambda Expressions (closures)?

is an anonymous

method

having an argument

list

a return type

A lambda expression :

a ->

Page 37: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

What are Lambda Expressions (closures)?

is an anonymous

method

having an argument

list

a return type and a body

A lambda expression :

a -> sum[0]+=a

Page 38: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

What are Lambda Expressions (closures)?

is an anonymous

method

having an argument

list

a return type and a body

A lambda expression :

a -> sum[0]+=a

and able to refer to values from the enclosing scope (closure)

Page 39: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

What are Lambda Expressions (closures)?

is an anonymous

method

having an argument

list

a return type and a body

A lambda expression :

a -> sum[0]+=a

and able to refer to values from the enclosing scope (closure)

To pass behavior to the API as data

Page 40: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

From Single Method Interface ….

public interface Comparator<T> {

}

Page 41: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

From Single Method Interface ….

public interface Comparator<T> {int compare(T o1, T o2);

}

Page 42: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

From Single Method Interface ….

public interface Comparator<T> {int compare(T o1, T o2);

}Single Abstract Method (SAM)

Page 43: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

From Single Method Interface ….

public interface Comparator<T> {int compare(T o1, T o2);

}Single Abstract Method (SAM)

Functional Interface

Page 44: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

From Single Method Interface ….

public interface Comparator<T> {int compare(T o1, T o2);

}

Collections.sort(strings, new Comparator<String>() {

public int compare(String s1, String s2) {return s1.compareToIgnoreCase(s2);

}});

Page 45: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

From Single Method Interface ….

public interface Comparator<T> {int compare(T o1, T o2);

}

Collections.sort(strings, new Comparator<String>() {

public int compare(String s1, String s2) {return s1.compareToIgnoreCase(s2);

}});

Page 46: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

From Single Method Interface ….

public interface Comparator<T> {int compare(T o1, T o2);

}

Collections.sort(strings, new Comparator<String>() {

public int compare(String s1, String s2) {return s1.compareToIgnoreCase(s2);

}});

Bulky syntax Confusion surrounding the meaning of names and this

Page 47: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

… To Lambda Expressions

Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2));

Page 48: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

… To Lambda Expressions

Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2));

Page 49: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

… To Lambda Expressions

Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2));

Lambda expression are always converted toinstance of a functional interface

Comparator<String> c = (s1, s2) -> s1.compareToIgnoreCase(s2));

No need of changing the JVM to create a newtype for lambda expressions

Page 50: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

… To Lambda Expressions

Collections.sort(strings,

new Comparator<String>() {public int

compare(String s1, String s2) {

return s1.compareToIgnoreCase(s2);}});

Collections.sort(strings,

(s1, s2) -> s1.compareToIgnoreCase(s2)

);

Page 51: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

… To Lambda Expressions

Collections.sort(strings,

new Comparator<String>() {public int

compare(String s1, String s2) {

return s1.compareToIgnoreCase(s2);}});

Collections.sort(strings,

(s1, s2) -> s1.compareToIgnoreCase(s2)

);

Simplicity

Page 52: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Without more language support for parallel idioms,people will instinctively reach for serial idioms

Page 53: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

External Iteration

Sum of squares

long sum = 0L;for (long i = 0; i < N; i++) {

sum += i * i;}

Page 54: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Internal Iteration

Sum of squares

long sum = LongStream.range(0, N).map(i -> i * i).sum();

Page 55: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Internal Iteration

Going parallel

long sum = LongStream.range(0, N).parallel().map(i -> i * i).sum();

Page 56: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Parallel Streams

A behavioral parameter (lambda) may be invoked concurrently– This constraint gives us “wiggle room” to optimize

Page 57: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Up the level of abstraction

External iteration conflates what with how

Internal iteration: more what; less how

Client passes behavior to the API as data

Library is free to use

Multiple threads

Out-of-order

executionLaziness

Page 58: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Lambdas in Java 8

Lambda

Code as data

(Simplicity)

Multi-core processing / parallel

processing

Interface evolution

Bulk data operations

Page 59: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Lambdas in Java 8

Page 60: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Date and Time APIJSR 310

Page 61: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

New Date and Time API

History of Java Date/Time APIs

Java.util.Date, java.util.Calendar- Strong candidates for the all-time worst Java platform library

design

Joda Time- Quality date and time library

JSR-310- Builds on experience of Joda Time

Page 62: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

New Date and Time API

Design Principles

Immutable- Thread-safe, allows caching

Fluent- easy to read, like a DSL

LocalDate.of(2013,Month.JANUARY,09).withYear(2014);

Page 63: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

New Date and Time API

Time for Humans

Field-base, designed for humans- Year,month,day,hour,minute,second- LocalDate,LocalDateTime,ZonedDateTime,Period …

Page 64: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

New Date and Time API

London

12:50

Los Angeles

16:20

Page 65: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

New Date and Time API

void flightTime(){

LocalDate date = LocalDate.of(2013, Month.SEPTEMBER, 14);

LocalTime takeoff = LocalTime.of(12, 50);LocalTime landing = LocalTime.of(16, 20);

ZoneId LHR = ZoneId.of("Europe/London");ZoneId SFO = ZoneId.of("America/Los_Angeles");

Duration flightTime = Duration.between(ZonedDateTime.of(date, takeoff, LHR),ZonedDateTime.of(date, landing, SFO));

System.out.println("Flight time: " + flightTime);}

Page 66: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

AnnotationsJSR 308

Page 67: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Type Annotations

JSR-308 brings annotations on Type use Are an enabler for the checkers framework

Ex.:

new @Interned MyObject();myString = (@NonNull String) myObject;void monitorTemperature() throws@Critical TemperatureException { ... }

Page 68: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Repeating Annotations

Before@Schedules ({

@Schedule(dayOfMonth="Last"),@Schedule(dayOfWeek="Fri",

hour="23"))}

public void doPeriodicCleanup() { ... }

Page 69: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Repeating Annotations

Before@Schedules ({

@Schedule(dayOfMonth="Last"),@Schedule(dayOfWeek="Fri",

hour="23"))}

public void doPeriodicCleanup() { ... }

After@Schedule(dayOfMonth="Last”)@Schedule(dayOfWeek="Fri", hour="23")public void doPeriodicCleanup() { ... }

Page 70: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Java SE 8 Compact Profiles

Page 71: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Java SE 8 Compact Profiles

SE Full JRE

Hotspot VM

Lang & Util Base Libraries

Other Base Libraries

Integration Libraries

UI & Toolkits

Optional Components

Hotspot VM

Base Compact1 Classes

SE 8 Compact Profiles

Compact2 Class libraries

Compact3 Class libraries

1

2

3

Page 72: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

Q&A

Page 73: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

References

• Lambdahttp://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambdahttp://openjdk.java.net/projects/lambda/http://jcp.org/aboutJava/communityprocess/edr/jsr335/index2.htmlhttp://vimeo.com/48577033 (slides: http://www.slideshare.net/tkowalcz/java-gets-a-closure)http://datumedge.blogspot.co.uk/2012/06/java-8-lambdas.htmlhttp://www.theserverside.com/news/thread.tss?thread_id=68718http://medianetwork.oracle.com/video/player/1785479333001https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=6080https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=5089http://www.lektorium.tv/lecture/?id=14048http://www.lektorium.tv/lecture/?id=14049http://blog.xebia.com/2012/11/05/report-will-java-8s-lambda-change-the-face-of-the-world/http://www.slideshare.net/fsarradin/java-8-lambdahttp://programmers.stackexchange.com/questions/173441/what-triggered-the-popularity-of-lambda-functions-in-modern-mainstream-programmi?newsletter=1&nlcode=29983%7c903ahttp://www.slideshare.net/bje/java-closures* Collectionshttp://www.javabeat.net/2012/05/enhanced-collections-api-in-java-8-supports-lambda-expressions/http://cr.openjdk.java.net/~briangoetz/lambda/collections-overview.htmlhttp://architects.dzone.com/articles/java-collections-api

Page 74: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

References

• Remove the Permanent Generationhttp://www.cubrid.org/blog/dev-platform/understanding-jvm-internals/http://javaeesupportpatterns.blogspot.com/2011/10/java-7-features-permgen-removal.htmlhttp://java.dzone.com/articles/busting-permgen-mythshttps://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=5135• JSR 310: Date and Time APIhttp://java.dzone.com/articles/introducing-new-date-and-timehttp://sourceforge.net/apps/mediawiki/threeten/index.php?title=ThreeTenhttp://www.infoq.com/news/2010/03/jsr-310https://docs.google.com/document/pub?id=1rd8yplQZIRz3LxMzpVLuskr1b0HwBmK9PXpdgBYojSwhttp://sourceforge.net/apps/mediawiki/threeten/index.php?title=User_Guidehttps://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=4350• General Java8http://openjdk.java.net/projects/jdk8/featureshttp://www.pcadvisor.co.uk/news/software/3401314/oracle-java-upgrades-still-worthwhile-despite-postponed-features/http://dhruba.name/2011/07/06/oracle-discusses-java-7-8-new-features-on-video/http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012/Java-8http://www.parleys.com/#st=5&id=2850&sl=1http://www.parleys.com/#st=5&id=2847https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=2872https://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=10458

Page 75: Mohsen Zainalpour zainalpour@yahoo.com What`s New in Java 8 JUG.

References

• Annotationshttps://oracleus.activeevents.com/connect/sessionDetail.ww?SESSION_ID=4469https://blogs.oracle.com/abuckley/entry/jsr_308_moves_forwardhttp://jcp.org/en/jsr/detail?id=308http://openjdk.java.net/jeps/120http://types.cs.washington.edu/checker-framework/