Getting ready to java 8

Post on 08-Sep-2014

879 views 1 download

Tags:

description

Сергей Моренец «Новые возможности Java 8», JUG#8 (Днепропетровск)

Transcript of Getting ready to java 8

Getting readyto Java 8

Сергей Моренец, morenets@mail.ru24 июля 2014 г.

About author• Works in IT since 2000• 11 year of Java SE/EE experience• Occupied senior Java developer/Team Lead

positions• Winner of 2013 JBoss Community Recognition

Award. https://www.jboss.org/jbcra

About author• 6 times speaker at Odessa Java User Group• 2 times speaker at JEEConf (2013 and 2014) in

Kiev• Speaker at Java Breeze 2013 in Odessa• Speaker at Java Day 2014 in Minsk• Works 2 years as Java lector

Java 8• Java 8 has been influenced by Scala much more

than how Scala has been influenced by Java• Most expected Java release ever

JVM-based languages

Functional languages

Java 8• Code name Kenai• Specification JSR-337• Inspired by Scala, Google Guava and Joda Time

Java 8• Project Lambda• Streams API• Functional interfaces• Java time• Method references• Repeatable & time annotations• Nashorn• Accumulators

Java 8• Method Thread.stop() was deprecated in 1998

and rewritten to throw exception in 2014.

Schedule• 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 Feature Complete• 2013/09/05 Developer Preview• 2014/01/23 Final Release Candidate• 2014/03/18 General Availability

Java 7

Java 7

Project Lambda

Project Lambda

Project Lambda

Java 7

Project Lambda

Project Lambda

Java 7

Project Lambda

Project Lambda

Project Lambda• Lambdas are not serializable• Applied to functional interfaces(with SAM)• Cannot use this• Not applied to abstract classes(even with SAM)

Project Lambda• Function• Predicate• Consumer• Supplier• BinaryOperator

Project Lambda

Interface DescriptionFunction Function with single

argument and resultPredicate Tests condition of

single argumentConsumer Performs operation

over single argumentSupplier Supplies results BinaryOperator Functions with two

arguments and results

Lambda vs Anonymous

Lambda vs Anonymous

Lambda vs Anonymous

Lambda vs Anonymous

Type Lambdas(ms)

Anonymous classes (ms)

Execution

364 329

Capturing

316 309

Linkage 354 313

Disadvantage

Java 7

Method reference

Method reference

Java 7

Streams

Streams

Streams

Streams

Java 7

Streams

Streams• Sequential• Parallel

Streams

Performance

PerformanceOperation Sequential,

secParallel,sec

Slow operation 1.0 0.4Quick operation 0.3 2.4

Parallelism level

Parallelism level• Parallel streams use ForkJoinPool to split and

combine tasks• By default number of threads is equal to number

of processors• This behavior may be overridden by changing

system property java.util.concurrent.ForkJoinPool.common.parallelism

Streams

Streams

Streams

Streams

Java 7

Streams

Stream performance

Stream performance

Iteration test

Use case Time(ms)For-each loop 35forEach(lambda) 79forEach(consumer) 12

Java 7

Default methods

Default methods

Default methods

Default methods• equals• hashCode• toString

Java 7

Java 7

Java 7

Optional

Optional

Optional

Construct date

Java 7

Java Time

Java Time

Java Time

Java Time

Java 7

Java Time

Time difference

Java Time

I/O

I/O

Encoding

Annotations

Annotations

Repeatable annotations

Repeatable annotations

Repeatable annotations

Annotations• Up to Java 7, an annotation was applied to a

declaration. • A declaration is a part of code that introduces a

new name.

Annotations• Type• Method• Field• Annotation• Package• Local variable

Type annotations

Type annotations• In Java 8, you can annotate any type use. • This can be useful in combination with tools that

check for common programming errors

Type annotations

Type annotations• Generic type arguments• Superclass and implemented interface• Constructor invocations• Casting and instanceof• Exception specifications• Wild card and type bounds

Type annotations

Nashorn

Nashorn• Based on Da Vinci Machine• Intends to enable Java developers embedding of

JavaScript in Java applications• Successor of JavaScript Rhino engine which was

started in 1997 by Netscape and maintained by Mozilla.

Java 7

Nashorn

Performance

Operation Rhino(Java 7) ,sec Nashorn (Java 8)print 3.1 0.9String operations 1.0 0.4Math operations 1.4 0.1

Collections• Improved HashMap• Balanced tree to avoid high collisions

Collections

Collections

Performance

Number of items Java 7,ms Java 81000 7 95000 67 4715000 653 40630000 2645 125250000 7842 3820

Accumulators• Scalable updatable variables. Maintaining a single

count, sum, etc., that is updated by possibly many threads is a common scalability problem.

• Added features that make ConcurrentHashMap and classes built from them more useful as caches. Better support for maps with large numbers of elements.

• Added functionality and improved performance for ForkJoinPool

Benchmark

Benchmark

Benchmark

Benchmark

Number of threads

AtomicLong LongAdder

1 236 1772 586 1384 590 468 501 4916 438 4832 459 52

Metaspace• Replacement of Permanent Generation• New JVM arguments• Resides in the heap and can be resized

Q&A

• Сергей Моренец, morenets@mail.ru