Andreas Enbohm Capgemini Sverige AB Scala or ”The Free Lunch is Over”

Slides:



Advertisements
Similar presentations
Concurrency The need for speed. Why concurrency? Moore’s law: 1. The number of components on a chip doubles about every 18 months 2. The speed of computation.
Advertisements

Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Chapter 3.2 C++, Java, and Scripting Languages. 2 C++ C used to be the most popular language for games Today, C++ is the language of choice for game development.
Erlang concurrency. Where were we? Finished talking about sequential Erlang Left with two questions  retry – not an issue; I mis-read the statement in.
Chapter 3.2 C++, Java, and Scripting Languages “The major programming languages used in game development.”
Based on Silberschatz, Galvin and Gagne  2009 Threads Definition and motivation Multithreading Models Threading Issues Examples.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
Generics, Proxy, and The Compile Time Type Checking Debate You are either with us or against us. Please snarf the code for today’s class.
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
Scala Actors -Terrance Dsilva.  Thankfully, Scala offers a reasonable, flexible approach to concurrency  Actors aren’t a concept unique to Scala.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
1-Sep-15 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
Groovy WHAT IS IT? HOW DOES IT WORK? IS IT USEFUL?
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2015 © 1.
A Brief Intro to Scala Tim Underwood. About Me Tim Underwood Co-Founder of Frugal Mechanic Software Developer Perl, PHP, C, C++, C#, Java, Ruby and now.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Jun 16, 2014IAT 2651 Debugging. Dialectical Materialism  Dialectical materialism is a strand of Marxism, synthesizing Hegel's dialectics, which proposes.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Threads, Thread management & Resource Management.
CS 390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Distributed Computing Fundamentals.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
1 Web Based Programming Section 8 James King 12 August 2003.
Best Practices. Contents Bad Practices Good Practices.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Title Line Subtitle Line Top of Content Box Line Top of Footer Line Left Margin LineRight Margin Line Top of Footer Line Top of Content Box Line Subtitle.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing with Graphics.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
What is a Servlet? Java Program that runs in a Java web server and conforms to the servlet api. A program that uses class library that decodes and encodes.
Threads, Thread management & Resource Management.
Peyman Dodangeh Sharif University of Technology Spring 2014.
6-Feb-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
CSS430 Threads1 Textbook Ch5 These slides were compiled from the OSC textbook slides (Silberschatz, Galvin, and Gagne) and the instructor’s class materials.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
13-Jun-16 Scala Apologia. 2 Java What’s wrong with Java? Not designed for highly concurrent programs The original Thread model was just wrong (it’s been.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
CSE S. Tanimoto Java Introduction 1 Java A Programming Language for Web-based Computing.
1 Chapter 5: Threads Overview Multithreading Models & Issues Read Chapter 5 pages
The heavyweight parts of lightweight languages LL1 Workshop November 17, 2001.
Java Thread Programming
OBJECT ORIENTED PROGRAMMING
Fibers – blocking is cheap in a Parallel Universe jPrime Stefan Minev
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
The heavyweight parts of lightweight languages
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Generics, Lambdas, Reflections
Advanced Programming Behnam Hatami Fall 2017.
Functional Programming with Java
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Scala Apologia 1-Jan-19.
Units with – James tedder
Student: Popa Andrei-Sebastian
Parallelism Can we make it faster? 8-May-19.
Chengyu Sun California State University, Los Angeles
CS 240 – Advanced Programming Concepts
Presentation transcript:

Andreas Enbohm Capgemini Sverige AB Scala or ”The Free Lunch is Over”

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Why Scala  The free lunch is over...  Today – 2 Cores  2010 maybe 12 (AMD), in 5 years maybe 16  Difficult to utilize several CPUs with Java  Huge API in current Java – some argue it may collape due to its own weight  Scala = ScalableLanguage  Designed to fit todays and some of tomorrows programming paradigms  Actors – Easier to utilize than memory synchronization (more about this later)  Nice (less boiler plate code) syntax and ’type safe duck typing’  And much more...

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Scala - Features  OO and functional language ”a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way”  Functional language – (often) no side effect, avoids state and mutable data, i.e functions can run independently and without knowlegde of eachother  Open Source  Compact code, ca 50 % less code than Java  Static typed language (but with ’kind of’ duck typing, more about this later)  Pattern matching, singel inheritance, immutable collections (like Java Strings), XML as FCC  SUnit, Lift (web framework), Support for Maven, Ant, Plugins for Eclipse, Netbeans, IntelliJ  Why not Ruby, Groovy, F#? - Fully interoperable with Java (call Java classes whenever you want) - Refactoring (static typed) - runs on world’s best VM! - very simple to inherit from Java classes - Javas annotations, threads, for each, it all works - Very fast (600x faster than groovy, lift 6x faster than Rails?) def sum(a:Int,b:Int) = a+b

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Scala – Compared to Java  Defining a class  Everything in Scala is an Object! This also includes functions class Person(var firstName: String, var lastName: String) { } public class Person { private String firstName; private String lastName; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; }

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Scala – Compared to Java  Defining variables - ; and type can often be omitted  Creating objects  List is of type List[String] by default  List objects in Scala contains several useful functions to access elements in the List  Ommiting some dots… val myInt: Int = 1; val anotherInt = 2 val anObject = Object //Omit () if you want just ’cluttering’ your code val aList = List(”John Doe”, ”Jane Doe”, ”Baby Doe”) println("Hi, " +aList.tail.head) //Prints “Hi, Jane Doe” List aList = new ArrayList (); //Java syntax for creating a list-object val aList = List //Scala syntax for creating corresponding list val aList = List[String] //Also works, notice just ONE generic definiton import java.util.{Date,Locale} import java.text.DateFormat import java.text.DateFormat._ val now = new Date val df = getDateInstance(LONG, Locale.FRANCE) println( df format now) //same as df.format(now)

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Scala – Compared to Java  Exceptions - Scala has no checked exception (unlike Java but like C#) - If used with Java, use  Using pattern matching and functions  Scala uses ’traits’ which can be compared to interfaces. Can have implementations (but no state) try { doSomething(params) } catch { case ex: IOException => println("Oops! Some file is probably missing") case ex: NullPointerException => println(“Dohh!! Must initialize the variable ") } object MatchTest1 extends Application { def matchTest(x: Int): String = x match { case 1 => "one" case 2 => "two" case _ => "many" } println(matchTest(1)) //Prints “one” println(matchTest(99)) //Prints “many” }

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Duck Typing in Scala  ”If it walks like a duck and quacks like a duck, then I would call it a duck.” – Wise Guy  Statically typed language don’t offer this flexibility (Java, C#) - i.e. no ’Duck Typing’  Downside – what happens if object does not have a ’quack’-method -> RuntimeException!  In Java, we must add an interface with method ’quack()’ – impossible to add an interface without changing the class! class Duck { public void quack() { print(”Duck Quacking!”) } class Person { public void quack() { print(”Person Quacking!”) } Duck d = new Duck(); Person p = new Person(); testQuack(d); testQucak(p); testQuack(duckableObject) { duckableObject.quack(); //Lets hope this work!!! }

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Duck Typing in Scala  However Scala offers ’Structual Typing’ – a.k.a. Type safe duck typing  Consider following example (Scala syntax)  Scala offers structual typing, i.e. def getName() in test  The structual type ’getName()’ checks at compile time that ’aFileObject’ has a getName() method if not – compile error  If getName() is needed more than one, use traits. class File(name: String) { def getName(): String = name def open() { /*..*/ } def close() { println("close file") } } def testPrintName(aFileObject: { def getName(): String }) { println(aFileObject.getName) } testPrintName(new File("test.txt")) testPrintName(new java.io.File("test.txt")) trait HasName { def getName() : String } def testPrintName(HasName f) = { println(f.getName) }

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Actors  ”Don't bring the grape juice into the living room“ – Brian Goetz  Java threads - shared memory, i.e. only one thread can operate on a shared resource concurrently - expensive to create, i.e. every thread has a high memory consumption - context switch. i.e a Java thread maps to a OS thread (limited numbers ~3K) - context switch make page faults - > slow due to thread needs to re-read data from RAM - difficult to use, i.e. deadlocks, raise conditions, live locks, NASA Mars explorer experied live lock - synchronize, i.e. not just a a monitor/lock but also write to main memory (no registers) ~100 slower! - Java was designed for single processors, not multi-cores!  Actor based concurrency - concurrenct processes communicates by exchanging messages - asynchronous message passing - ’share-nothing’-model - Erlang early to implement this style of concurrency - Erlang based web server ~80000 clients, Apache ~4000 clients  Two flavours in Scala; - Thread based actors – high level abstraction of threads which replaces error-prone shared memory access - Thread less actors – offers enourmous scalability. Lightweight processes are used as actors with much less overhead than a usual thread. ’Piggy-backs’ on calling threads stack trace (continuation closure)

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Downsides  ”With great power comes great complexity” – James Gosling  New languages difficult to introduce to organisations  Not ready for prime time?

© 2004 Capgemini - All rights reserved 2008, July – Andreas Enbohm Questions?