Java Volatile Variables: Example Application
[Pages:11]Java Volatile Variables: Example Application
Douglas C. Schmidt
d.schmidt@vanderbilt.edu dre.vanderbilt.edu/~schmidt
Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA
Learning Objectives in this Part of the Lesson
? Understand how Java volatile variables provide concurrent programs with thread-safe mechanisms to read from & write to single variables
? Know how to use a Java volatile variable in practice
class Singleton { private static volatile Singleton sInst = null; public static Singleton instance() { Singleton result = sInst; if (result == null) { synchronized(Singleton.class) { result = sInst; if (result == null) sInst = result = new Singleton(); } } return result; ...
2
Using a Java Volatile Variable in Practice
3
Using a Java Volatile Variable in Practice
? Volatile is relatively simple & efficient means to ensure atomic reads & writes
class Singleton { private static volatile Singleton sInst = null; public static Singleton instance() { Singleton result = sInst; if (result == null) { synchronized(Singleton.class) { result = sInst; if (result == null) sInst = result = new Singleton(); } } return result; ...
4
Using a Java Volatile Variable in Practice
? Volatile is relatively simple & efficient means to ensure atomic reads & writes
? e.g., it can be used to
implement the DoubleChecked Locking pattern
class Singleton { private static volatile Singleton sInst = null; public static Singleton instance() { Singleton result = sInst; if (result == null) { synchronized(Singleton.class) { result = sInst; if (result == null) sInst = result = new Singleton(); } } return result; ...
See en.wik5i/Double-checked_locking
Using a Java Volatile Variable in Practice
? Volatile is relatively simple & efficient means to ensure atomic reads & writes ? e.g., it can be used to
implement the DoubleChecked Locking pattern
Reduces locking overhead via "lazy initialization" in a multi-threaded environment
class Singleton { private static volatile Singleton sInst = null; public static Singleton instance() { Singleton result = sInst; if (result == null) { synchronized(Singleton.class) { result = sInst; if (result == null) sInst = result = new Singleton(); } } return result; ...
See en.6/wiki/Lazy_initialization
Using a Java Volatile Variable in Practice
? Volatile is relatively simple & efficient means to ensure atomic reads & writes ? e.g., it can be used to
implement the DoubleChecked Locking pattern
Ensures just the right amount of synchronization
class Singleton { private static volatile Singleton sInst = null; public static Singleton instance() { Singleton result = sInst; if (result == null) { synchronized(Singleton.class) { result = sInst; if (result == null) sInst = result = new Singleton(); } } return result; ...
7
Using a Java Volatile Variable in Practice
? Volatile is relatively simple & efficient means to ensure atomic reads & writes ? e.g., it can be used to
implement the DoubleChecked Locking pattern
Only synchronizes when sInst is null, i.e., the "first time in"
class Singleton { private static volatile Singleton sInst = null; public static Singleton instance() { Singleton result = sInst; if (result == null) { synchronized(Singleton.class) { result = sInst; if (result == null) sInst = result = new Singleton(); } } return result; ...
8
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- introduction to the java programming language
- oracle applications using java jdbc
- java conditionobject example application
- java volatile variables example application
- chapter14 graphical user interfaces
- java by example semantic scholar
- chapter 2 simple java applications people
- building trading applications using the oracle extreme
- java application developer s guide marklogic
Related searches
- example of college application essay
- java array example code
- java application example
- java application examples pdf
- java application source code
- java database application example
- java example code for beginners
- example of java program
- example java code hello world
- java class example code
- arraylist java exam example code
- java arraylist example code