Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 313 – Advanced Programming Topics.

Similar presentations


Presentation on theme: "Computer Science 313 – Advanced Programming Topics."— Presentation transcript:

1 Computer Science 313 – Advanced Programming Topics

2 How About that Recursion Recursive profile of 0.67 secs (32 total ticks):main Interpreted + native Method 3.1% 1 + 0 Recursive02.binomialExpansion Compiled + native Method 96.9% 26 + 5 Recursive02.binomialExpansion Iterative profile of 2.61 secs (145 total ticks):main Interpreted + native Method 0.7% 1 + 0 java.util.Vector.elementAt Compiled + native Method 85.5% 118 + 6 Recursive02.binomialExpansion 13.8% 20 + 0 java.util.Vector.addElement

3 What Happened? Why?

4 Simple Explanation  Java’s implementation of Stack sucks

5 Real Explanation  Java’s Stack slower than system stack  Well, duh, but more important is why  Stack class must work in lots of situations  Includes checks to grow on each call to push  Must balance size and speed demands  Okay performance often; great performance never  System stack is highly specialized  Explodes size to gain speed  Benefits from knowing it has only one use

6 Singleton Pattern

7 Singleton Pattern Guides D O NOT use for  1 instance wanted  Global value  Instead use static field  Poor design suggested  Instantiation check  Use factory method D O use for  1 instance possible  Global access point  For a global resource  Gatekeeper for actions  Optimize performance  Can be easily removed

8 Common Themes in Singleton  Every Singleton includes following members  static field holding the instance  To get instance defines public static method  Singleton usually includes this member also  Limit instantiation defining private constructor  May include in class, but much rarer  Allow subclassing with protected constructor  Getter as simple factory also found in this case

9 How To Create Factory Singleton public class GameFactory { private static GameFactory me; protected GameFactory() { } public static GameFactory instance(int userAge){ if (me == null) { if (userAge < 13) me = new AnimatedGameFactory(); else me = new HardCoreGameFactory(); } return me; } }

10 Oops, Our Bad… public class PrinterSpool { private static PrinterSpool me; private PrinterSpool() { } public static PrinterSpool instance() { if (me == null) { me = new PrinterSpool(); } return me; } }

11 Synchronization is Hard

12 When Method Rarely Called  Explicit, but expensive, synchronization used public class Engine { private static Engine me; private Engine() { } public static Engine instance() { if (me == null) { me = new Engine(); } return me; } }

13 When Method Rarely Called  Explicit, but expensive, synchronization used  Synchronized routines performed at each call synchronized public class Engine { private static Engine me; private Engine() { } public static synchronized Engine instance() { if (me == null) { me = new Engine(); } return me; } }

14 When Instantiation is Cheap  Uses system’s implicit synchronization public class Engine { private static Engine me; private Engine() { } public static Engine instance() { if (me == null) { me = new Engine(); } return me; } }

15 When Instantiation is Cheap  Uses system’s implicit synchronization  Eagerly create instance & keep it until needed public class Engine { private static Engine me = new Engine(); private Engine() { } public static Engine instance() { return me; } }

16 For Use on Any JVM  Also uses system’s implicit synchronization public class Engine { private static Engine me; private Engine() { } public static Engine instance() { if (me == null) { me = new Engine(); } return me; } }

17 For Use on Any JVM  Also uses system’s implicit synchronization  But delays creation instance until needed public class Engine { private class EngineHolder { private static Engine instance=new Engine(); } private static Engine me; private Engine() { } public static Engine instance() { return EngineHolder.instance; } }

18 On Modern (1.5+) JVMs  Double-locking only costs on instance creation public class Engine { private static Engine me; private Engine() { } public static Engine instance() { if (me == null) { me = new Engine(); } return me; } }

19 On Modern (1.5+) JVMs  Double-locking only costs on instance creation  Accessing static field expensive for rest of program public class Engine { private volatile static Engine me; private Engine() { } public static Engine instance() { if (me == null) synchronized (Engine.class) { if (me == null) { me = new Engine(); } } return me; } }

20 For Next Class  Lab #5 on Angel  Have 1.5 weeks left to complete this lab  Do not delay, it will take time to complete  Read article linked to from Angel page  Describes how inlining works in Java  Why getters & setters are cheap explained  Shows how optimization cascades to generate really, really big improvements


Download ppt "Computer Science 313 – Advanced Programming Topics."

Similar presentations


Ads by Google