Week 4 Day 1 Singletons l l l

  • Slides: 8
Download presentation
Week 4, Day 1: Singleton(s? ) l l l Happy New Year! Are you

Week 4, Day 1: Singleton(s? ) l l l Happy New Year! Are you warm enough? Review: l l l Static variables and methods Threads A Singleton l l l What is a Singleton? Two kinds of Singletons Multithreading and Singletons Lab Wednesday: Cumulative Quiz SE-2811 1 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick

Static variables and methods l l l See example Static. Stuff What does the

Static variables and methods l l l See example Static. Stuff What does the code print? Why? Draw a picture of what memory looks like at the end of the program. SE-2811 Dr. Mark L. Hornick 2

Bad Threads l Write out a multithreaded-trace of this program. l What does the

Bad Threads l Write out a multithreaded-trace of this program. l What does the code print? SE-2811 Dr. Mark L. Hornick 3

Have you ever seen a private Constructor? class Mystery { private Mystery() { /*

Have you ever seen a private Constructor? class Mystery { private Mystery() { /* */ } } l What happens if a constructor is private? l Why would you want this? SE-2811 Dr. Mark L. Hornick 4

A Singleton l A Singleton is a class with only one instance l Work

A Singleton l A Singleton is a class with only one instance l Work Example. java and Singleton. SE-2811 Dr. Mark L. Hornick 5

Two kinds of Singletons l Eager Lazy l Continue coding examples l SE-2811 Dr.

Two kinds of Singletons l Eager Lazy l Continue coding examples l SE-2811 Dr. Mark L. Hornick 6

The Locking Problem l How can more than one singleton be created? In class

The Locking Problem l How can more than one singleton be created? In class example. l How to make sure only one singleton is created? SE-2811 Dr. Mark L. Hornick 7

Fixing the Locking Problem l In-class example SE-2811 Dr. Mark L. Hornick 8

Fixing the Locking Problem l In-class example SE-2811 Dr. Mark L. Hornick 8