Download presentation
Presentation is loading. Please wait.
Published byBrent Cain Modified over 9 years ago
1
Threads and Singleton
2
Threads The JVM allows multiple “threads of execution” Essentially separate programs running concurrently in one memory space No inherent guarantees about the order in which different threads execute Threads take turns executing and one may be stopped to let another run at any time The JVM allows multiple “threads of execution” Essentially separate programs running concurrently in one memory space No inherent guarantees about the order in which different threads execute Threads take turns executing and one may be stopped to let another run at any time
3
Dining Philosophers Five philosophers around a table with five forks spread between them All philosophers do is think and eat To eat, a philosopher must hold both of the forks next to it. Can you cause deadlock? Which rules for the philosophers cause deadlocks and which ones prevent it? Five philosophers around a table with five forks spread between them All philosophers do is think and eat To eat, a philosopher must hold both of the forks next to it. Can you cause deadlock? Which rules for the philosophers cause deadlocks and which ones prevent it?
4
Look at Code Each philosopher is a separate thread The state of the forks is shared (static) Will it run to completion? Each philosopher is a separate thread The state of the forks is shared (static) Will it run to completion?
5
Preventing deadlock Pick up lower numbered fork Pick up higher numbered fork Eat Put down higher numbered fork Put down lower numbered fork Think Forever Pick up lower numbered fork Pick up higher numbered fork Eat Put down higher numbered fork Put down lower numbered fork Think Forever
6
Singleton Used when you only want one instance of a class Make constructor private and, instead, give everyone a reference to the same instance Used when you only want one instance of a class Make constructor private and, instead, give everyone a reference to the same instance
7
Example Check out the code in Singleton
8
public class Professor { private boolean isTeaching; // other variables private static Professor uniqueProfessor; private Professor() { isTeaching = false; //other assignments } public static Professor getProfessor() { if (uniqueProfessor == null) { uniqueProfessor = new Professor(); return uniqueProfessor; } return uniqueProfessor; } //other methods }
9
Singleton Pattern Definition The Singleton Pattern ensures a class has only one instance, and provides a global point of access to that instance.
10
Singleton Pattern UML Singleton static uniqueInstance private Singleton() static getInstance()
11
Why Did I Show You Threads? What happens if we have multiple threads getting singletons? Is there a place a thread could be interrupted that would cause problems? What happens if we have multiple threads getting singletons? Is there a place a thread could be interrupted that would cause problems?
12
Synchronized Adding the keyword “synchronized” to the definition of a method means that only one thread can be executing that method at any point in time Adding Synchonized to the getProfessor would certainly fix our problem Has a non-trivial performance penalty Adding the keyword “synchronized” to the definition of a method means that only one thread can be executing that method at any point in time Adding Synchonized to the getProfessor would certainly fix our problem Has a non-trivial performance penalty
13
public class SyncProfessor { private boolean isTeaching; // other variables private static SyncProfessor uniqueProfessor; private SyncProfessor() { isTeaching = false; //other assignments } private static synchronized SyncProfessor getProfessor() { if (uniqueProfessor == null) { uniqueProfessor = new SyncProfessor(); } return uniqueProfessor; } //other methods }
14
Eager Creation vs. Lazy Creation Eager: initialize at variable declaration (created when the class is loaded) WATCH OUT! If construction depends on information not available at load time (constructor parameters), can’t use eager construction. Eager: initialize at variable declaration (created when the class is loaded) WATCH OUT! If construction depends on information not available at load time (constructor parameters), can’t use eager construction.
15
Double Checked Locking Look at DoubleCheckedProfessor “Volatile” marks a member variable not to be used in optimization, during compilation “synchronized” obtains and releases locks on monitors “volatile” only available with Java 1.5 and later! Look at DoubleCheckedProfessor “Volatile” marks a member variable not to be used in optimization, during compilation “synchronized” obtains and releases locks on monitors “volatile” only available with Java 1.5 and later!
16
public class DoubleCheckedProfessor { private boolean isTeaching; private volatile static DoubleCheckedProfessor uniqueProfessor; private DoubleCheckedProfessor() { isTeaching = false; } private static DoubleCheckedProfessor getProfessor() { if (uniqueProfessor == null) { synchronized (DoubleCheckedProfessor.class) { if (uniqueProfessor == null) { uniqueProfessor = new DoubleCheckedProfessor(); } return uniqueProfessor; } //other methods }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.