Mohsen Zainalpour What`s New in Java 8 JUG.

Slides:



Advertisements
Similar presentations
1 Copyright © 2005, Oracle. All rights reserved. Introducing the Java and Oracle Platforms.
Advertisements

Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
Writing Modern C++ Marc Grégoire Software Architect April 3 rd 2012.
CS18000: Problem Solving and Object-Oriented Programming.
Inheritance Inheritance Reserved word protected Reserved word super
Lab#1 (14/3/1431h) Introduction To java programming cs425
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
Objects and Classes First Programming Concepts. 14/10/2004Lecture 1a: Introduction 2 Fundamental Concepts object class method parameter data type.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Principles of Object-Oriented Software Development The language Java.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
Lecture 2: Design and Implementation of Lambda Expressions in Java 8 Alfred V. Aho CS E6998-1: Advanced Topics in Programming Languages.
Intro to C# Language Richard Della Tezra IS 373. What Is C#? C# is type-safe object-oriented language Enables developers to build a variety of secure.
2 2 The following is intended to outline our general product direction. It is intended for informational purposes only, and may not be incorporated into.
PARALLEL PROGRAMMING ABSTRACTIONS 6/16/2010 Parallel Programming Abstractions 1.
Main sponsor. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. 2 Project Lambda: Functional Programming Constructs & Simpler Concurrency.
Appendix B: Lambda Expressions In the technical keynote address for JavaOne 2013, Mark Reinhold, chief architect for the Java Platform Group at Oracle,
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Parallel Programming in Java with Shared Memory Directives.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Advanced Programming Collage of Information Technology University of Palestine, Gaza Prepared by: Mahmoud Rafeek Alfarra Lecture 2: Major Concepts of Programming.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 11: Compiler II: Code Generation slide 1www.idc.ac.il/tecs.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
ServiceSs, a new programming model for the Cloud Daniele Lezzi, Rosa M. Badia, Jorge Ejarque, Raul Sirvent, Enric Tejedor Grid Computing and Clusters Group.
23-October-2002cse JavaIntro © 2002 University of Washington1 Intro to Java CSE 413, Autumn 2002 Programming Languages
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
Lambda Expressions In the technical keynote address for JavaOne 2013, Mark Reinhold, chief architect for the Java Platform Group at Oracle, described lambda.
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
AUTO-GC: Automatic Translation of Data Mining Applications to GPU Clusters Wenjing Ma Gagan Agrawal The Ohio State University.
CMSC 330: Organization of Programming Languages Operational Semantics.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Functional Processing of Collections (Advanced) 6.0.
JAVA TRAINING IN NOIDA. JAVA Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented and specifically.
PySpark Tutorial - Learn to use Apache Spark with Python
NFV Compute Acceleration APIs and Evaluation
A brief intro to: Parallelism, Threads, and Concurrency
Topic: Java Garbage Collection
Before You Begin Nahla Abuel-ola /WIT.
Taking Java from purely OOP by adding “functional level Programming”
Methods Chapter 6.
Java 9 New features 8/11/2017 Iason Dimitrios Rodis.
Functional Programming with Java
Java 8 Java 8 is the biggest change to Java since the inception of the language Lambdas are the most important new addition Java is playing catch-up: most.
Embracing Java 9 and beyond with Eclipse JDT
CISC101 Reminders Slides have changed from those posted last night…
Functional interface.
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
(Computer fundamental Lab)
Computer Science 312 What’s New in Java 8? 1.
By Rajanikanth B Overview Of Java By Rajanikanth B
„Lambda expressions, Optional”
SPL – PS1 Introduction to C++.
Chengyu Sun California State University, Los Angeles
Threads and concurrency / Safety
Presentation transcript:

Mohsen Zainalpour What`s New in Java 8 JUG

2012/04/26M1 2012/06/14M2 2012/08/02M3 2012/09/13M4 2012/11/29M5 2013/01/31M6 2013/06/13M7Feature Complete 2013/09/05M8Developer Preview 2014/01/23M9Final Release Candidate 2014/03/18GAGeneral Availability JDK 8 Schedule and status

Major changes Agenda JUG Why is Java still changing? Type Annotations Lambda Project Date and Time API Compact Profiles

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 )

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 )

Lambda JSR 335

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 parallel processing big datamulticore cloud computing

The Progress of Programming There`s been a lot of progress & Progress is being allowed to forget things!

The Progress of Programming There`s been a lot of progress: -Assemblers let us forget opcodes

The Progress of Programming There`s been a lot of progress: -Assemblers let us forget opcodes -Garbage collections let us forget memory management #include int main () { … buffer = (char*) malloc (i+1); … free (buffer); … }

The Progress of Programming So what about parallelism?

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]; } int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; }  It is inherently serial

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

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 n1n2n3n4n5n6n7n8n9………… sum1 sum2 sum3sum4

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 n1n2n3n4n5n6n7n8n9………… class Sum implements Callable { 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 sum3sum4

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 n1n2n3n4n5n6n7n8n9………… sum1 sum2 sum3sum4 ExecutorService executor = Executors.newFixedThreadPool(2); List > results = executor.invokeAll(asList( new Sum(1, 250), new Sum(251, 500), new Sum(551, 750), new Sum(751, 1000) )); for (Future result : results) { System.out.println(result.get()); }

Why Can`t We Forget About Parallelism? Sequential Parallel (assuming a quad core machine) Time t Thread 1 Thread 2 Thread 3 Thread 4 Done parallel Done sequential

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

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 itr = myList.iterator();itr.hasNext(); ) { sum += itr.next(); } int sum = 0; for (Iterator itr = myList.iterator();itr.hasNext(); ) { sum += itr.next(); } The problem is external iteration

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; } Instead of this: int sum = 0; for (int i=0;i<myList.size();i++){ int a=myList.get(i); sum+=a; }

Why Can`t We Forget About Parallelism? Internal Iteration We`re going to write this: int [] sum = new int[1]; myList.forEach( … ); 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; } Instead of this: int sum = 0; for (int i=0;i<myList.size();i++){ int a=myList.get(i); sum+=a; }

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( … ); 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; } Instead of this: int sum = 0; for (int i=0;i<myList.size();i++){ int a=myList.get(i); sum+=a; }

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 ); 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; } Instead of this: int sum = 0; for (int i=0;i<myList.size();i++){ int a=myList.get(i); sum+=a; }

Why Can`t We Forget About Parallelism? a -> sum[0]+=a

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.

What are Lambda Expressions (closures)? A lambda expression :

What are Lambda Expressions (closures)? is an anonymous method A lambda expression :

What are Lambda Expressions (closures)? is an anonymous method having an argument list A lambda expression : a ->

What are Lambda Expressions (closures)? is an anonymous method having an argument list a return type A lambda expression : a ->

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

What are Lambda Expressions (closures)? is an anonymous method having an argument list a return type and a body A lambda expression : sum[0] a -> sum[0]+=a and able to refer to values from the enclosing scope (closure)

What are Lambda Expressions (closures)? is an anonymous method having an argument list a return type and a body A lambda expression : sum[0] a -> sum[0]+=a and able to refer to values from the enclosing scope (closure) To pass behavior to the API as data

From Single Method Interface …. public interface Comparator { }

From Single Method Interface …. public interface Comparator { int compare(T o1, T o2); }

From Single Method Interface …. public interface Comparator { int compare(T o1, T o2); } Single Abstract Method (SAM)

From Single Method Interface …. public interface Comparator { int compare(T o1, T o2); } Single Abstract Method (SAM) Functional Interface

From Single Method Interface …. public interface Comparator { int compare(T o1, T o2); } Collections.sort(strings, new Comparator () { public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } });

From Single Method Interface …. public interface Comparator { int compare(T o1, T o2); } Collections.sort(strings, new Comparator () { public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } });

From Single Method Interface …. public interface Comparator { int compare(T o1, T o2); } Collections.sort(strings, new Comparator () { public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } });  Bulky syntax  Confusion surrounding the meaning of names and this

… To Lambda Expressions Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2));

… To Lambda Expressions Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2));

… To Lambda Expressions Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2)); Lambda expression are always converted to instance of a functional interface Comparator c = (s1, s2) -> s1.compareToIgnoreCase(s2)); No need of changing the JVM to create a new type for lambda expressions

… To Lambda Expressions Collections.sort(strings, new Comparator () { public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } }); Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2) );

… To Lambda Expressions Collections.sort(strings, new Comparator () { public int compare(String s1, String s2) { return s1.compareToIgnoreCase(s2); } }); Collections.sort(strings, (s1, s2) -> s1.compareToIgnoreCase(s2) );

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

External Iteration Sum of squares long sum = 0L; for (long i = 0; i < N; i++) { sum += i * i; }

Internal Iteration Sum of squares long sum = LongStream.range(0, N).map(i -> i * i).sum();

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

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

Up the level of abstraction External iteration conflates what with howInternal iteration: more what; less howClient passes behavior to the API as dataLibrary is free to use Multiple threads Out-of- order execution Laziness

Lambdas in Java 8 Lambda Code as data (Simplicity) Multi-core processing / parallel processing Interface evolution Bulk data operations

Lambdas in Java 8

Date and Time API JSR 310

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 Builds on experience of Joda Time

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);

New Date and Time API Time for Humans Field-base, designed for humans -Year,month,day,hour,minute,second -LocalDate,LocalDateTime,ZonedDateTime,Period …

New Date and Time API London 12:50 Los Angeles 16:20

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); }

Annotations JSR 308

Type Annotations  JSR-308 brings annotations on Type use  Are an enabler for the checkers framework Ex.: MyObject(); myString = String) myObject; void monitorTemperature() TemperatureException {... }

Repeating Annotations  ({ hour="23") )} public void doPeriodicCleanup() {... }

Repeating Annotations  ({ hour="23") )} public void doPeriodicCleanup() {... } hour="23") public void doPeriodicCleanup() {... }

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

Q&A

References Lambda (slides: mainstream-programmi?newsletter=1&nlcode=29983% 7c903a * Collections

References Remove the Permanent Generation JSR 310: Date and Time API General Java

References Annotations