SlideShare una empresa de Scribd logo
1 de 17
Objects Constructed within the
   Final Field Safe Context

 Tibor Digana
Normal Field on an Instance
  What must be the value in provider.f
                Thread 2                    Thread 1

                           provider = new FinalFieldExample();

<read> provider.f;         class FinalFieldExample {

                               private Object f;

                               FinalFieldExample() {
                                   f = new Object();
                               }

                           }
Normal Field on an Instance
 What must be the value in provider.f

• No guarantee of proper visibility of values by a
  read in <f> in Thread 2.
Final Field on an Instance
   What must be the value in provider.f
                Thread 2                    Thread 1

                           provider = new FinalFieldExample();

<read> provider.f;         class FinalFieldExample {

                               private final Object f;

                               FinalFieldExample() {
                                   f = new Object();
                               }

                           }
Final Field on an Instance
 What must be the value in provider.f

• The visibility of values in <f> is guaranteed by
  a read in Thread 2.
Volatile field on an Instance
   What must be the value in provider.f
                Thread 2                    Thread 1

                           provider = new FinalFieldExample();

<read> provider.f;         class FinalFieldExample {

                               private volatile Object f = null;

                               FinalFieldExample() {
                                   f = new Object();
                               }

                           }
Volatile field on an Instance
 What must be the value in provider.f
• No guarantee of proper visibility of values in
  <f> by a read in Thread 2.
• Constructor’s freeze and the guarantees apply
  only to objects with final fields.
• An object is considered to be completely
  initialized when its constructor finishes.
Dereferenced Object in other Thread
Thread 2          Thread 1

Use of s.         s = new String()

                  An implementation detail.

                  The java.lang.String has private members
                                 char value[]
                                   int offset
                                   int count
                                    int hash
Dereferenced Object in other Thread
• Broken JMM in JDK 1.4 and earlier.
• The immutable object (of java.lang.String as a
  real example) has a proper visibility of the
  values in internal fields into other threads due
  to fixes in JMM since JDK 1.5.
Combined Fields on an Instance
         What must be the values.
                Thread 2                    Thread 1

                           provider = new FinalFieldExample();

<read> provider.f;         class FinalFieldExample {
<read> provider.x;
                               private final Object f;
                               private Object x;

                               FinalFieldExample() {
                                   x = new Object();
                                   f = new Object();
                               }

                           }
Combined Fields on an Instance
      What must be the values.
• Suppose<provider> is non-final field, non-null.
• The value which is set to <x> in the
  constructor has improper visibility in Thread 2.
• Value assigned to <f> is properly visible to
  Thread 2.
• Dereference and Memory Chains for the read
  of <f> in Thread 2 can pass through any reads
  by Thread 2 of a reference to <provider>.
Combined Fields on an Instance
         What must be the values.
                Thread 2                     Thread 1

                           provider = new FinalFieldExample();

<read> provider.f;         class FinalFieldExample {
<read> provider.g;              private final Object f;
<read> provider.a;              private final Object g;
<read> provider.b;              private Object a, b, c, d;
<read> provider.c;
<read> provider.d;             FinalFieldExample() {
                                   a = new Object();
                                   b = new Object();
                                   f = new Object();
                                   c = new Object();
                                   g = new Object();
                                   d = new Object();
                               }
                           }
Combined Fields on an Instance
      What must be the values.
• Values in a, b, c, and d have no guarantee to
  be properly visible by a read in Thread 2.
  (see previous slide)
• Since the <f> and <g> are final, their
  operations cannot be reordered with other.
• Operations on <a>, <b> can be reordered.
• <c> is always between <f> and <g>.
• <d> stays behind <g>
Reflection on Final Field
     What must be the value in f

         DO NOT USE IN CONCURRENT CODE
private final Object f = …;

getClass().getField("f").set(this, new Object());

return f;
Reflection on Final Field
       What must be the value in f
• If a final field is initialized to a compile-time
  constant in the field declaration, changes to
  the final field may not be observed, since uses
  of that final field are replaced at compile time
  with the compile-time constant.
Aggressive optimization of final fields.
• Within a thread, it is permissible to reorder
  reads of a final field with calls to methods that
  may change final fields via reflection.
• Reflection can be used to change final fields
  after the constructor for the object completes.
Aggressive optimization of final fields.
• A method which only returns a value held by
  final field will be inlined with compile-time
  value into method’s caller code;
• Static methods and private instance methods
  are considered final and inlined;
• Method code smaller than 35 bytes in byte
  code used to be inlined (-XX:MaxInlineSize)
  and therefore a subject of further reordering.

Más contenido relacionado

La actualidad más candente

Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++NUST Stuff
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++Sachin Yadav
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
Function class in c++
Function class in c++Function class in c++
Function class in c++Kumar
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
PARAMETER PASSING MECHANISMS
PARAMETER PASSING MECHANISMSPARAMETER PASSING MECHANISMS
PARAMETER PASSING MECHANISMSArpee Callejo
 
Pointer and polymorphism
Pointer and polymorphismPointer and polymorphism
Pointer and polymorphismSangeethaSasi1
 

La actualidad más candente (20)

Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
Scala functions
Scala functionsScala functions
Scala functions
 
Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++Lecture#7 Call by value and reference in c++
Lecture#7 Call by value and reference in c++
 
Functions
FunctionsFunctions
Functions
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Iterations and Recursions
Iterations and RecursionsIterations and Recursions
Iterations and Recursions
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Function class in c++
Function class in c++Function class in c++
Function class in c++
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
PARAMETER PASSING MECHANISMS
PARAMETER PASSING MECHANISMSPARAMETER PASSING MECHANISMS
PARAMETER PASSING MECHANISMS
 
Pointers
PointersPointers
Pointers
 
Pointer and polymorphism
Pointer and polymorphismPointer and polymorphism
Pointer and polymorphism
 

Destacado

RBSC Company Overview
RBSC Company OverviewRBSC Company Overview
RBSC Company Overviewkaufmajs
 
Building A Writers' Portal: 3 Months - 2 Guys - 1 CMS - 0 Money
Building A Writers' Portal: 3 Months - 2 Guys - 1 CMS - 0 MoneyBuilding A Writers' Portal: 3 Months - 2 Guys - 1 CMS - 0 Money
Building A Writers' Portal: 3 Months - 2 Guys - 1 CMS - 0 MoneyJeremy Snell
 
Speed Technology Dating from Internet Librarian 2012
Speed Technology Dating from Internet Librarian 2012Speed Technology Dating from Internet Librarian 2012
Speed Technology Dating from Internet Librarian 2012Jeremy Snell
 
My first experience with lambda expressions in java
My first experience with lambda expressions in javaMy first experience with lambda expressions in java
My first experience with lambda expressions in javaScheidt & Bachmann
 
Friendly, Positive, Energetic
Friendly, Positive, EnergeticFriendly, Positive, Energetic
Friendly, Positive, Energeticshaanapalmer
 
Intro To Mass Communications/ Slide Pres. #1
Intro To Mass Communications/ Slide Pres. #1Intro To Mass Communications/ Slide Pres. #1
Intro To Mass Communications/ Slide Pres. #1shaanapalmer
 

Destacado (9)

Ant Works Eng
Ant Works EngAnt Works Eng
Ant Works Eng
 
RBSC Company Overview
RBSC Company OverviewRBSC Company Overview
RBSC Company Overview
 
Building A Writers' Portal: 3 Months - 2 Guys - 1 CMS - 0 Money
Building A Writers' Portal: 3 Months - 2 Guys - 1 CMS - 0 MoneyBuilding A Writers' Portal: 3 Months - 2 Guys - 1 CMS - 0 Money
Building A Writers' Portal: 3 Months - 2 Guys - 1 CMS - 0 Money
 
Speed Technology Dating from Internet Librarian 2012
Speed Technology Dating from Internet Librarian 2012Speed Technology Dating from Internet Librarian 2012
Speed Technology Dating from Internet Librarian 2012
 
My first experience with lambda expressions in java
My first experience with lambda expressions in javaMy first experience with lambda expressions in java
My first experience with lambda expressions in java
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Talk With Me
Talk With MeTalk With Me
Talk With Me
 
Friendly, Positive, Energetic
Friendly, Positive, EnergeticFriendly, Positive, Energetic
Friendly, Positive, Energetic
 
Intro To Mass Communications/ Slide Pres. #1
Intro To Mass Communications/ Slide Pres. #1Intro To Mass Communications/ Slide Pres. #1
Intro To Mass Communications/ Slide Pres. #1
 

Último

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 

Último (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Java Objects Constructed Within The Final Field Safe Context

  • 1. Objects Constructed within the Final Field Safe Context Tibor Digana
  • 2. Normal Field on an Instance What must be the value in provider.f Thread 2 Thread 1 provider = new FinalFieldExample(); <read> provider.f; class FinalFieldExample { private Object f; FinalFieldExample() { f = new Object(); } }
  • 3. Normal Field on an Instance What must be the value in provider.f • No guarantee of proper visibility of values by a read in <f> in Thread 2.
  • 4. Final Field on an Instance What must be the value in provider.f Thread 2 Thread 1 provider = new FinalFieldExample(); <read> provider.f; class FinalFieldExample { private final Object f; FinalFieldExample() { f = new Object(); } }
  • 5. Final Field on an Instance What must be the value in provider.f • The visibility of values in <f> is guaranteed by a read in Thread 2.
  • 6. Volatile field on an Instance What must be the value in provider.f Thread 2 Thread 1 provider = new FinalFieldExample(); <read> provider.f; class FinalFieldExample { private volatile Object f = null; FinalFieldExample() { f = new Object(); } }
  • 7. Volatile field on an Instance What must be the value in provider.f • No guarantee of proper visibility of values in <f> by a read in Thread 2. • Constructor’s freeze and the guarantees apply only to objects with final fields. • An object is considered to be completely initialized when its constructor finishes.
  • 8. Dereferenced Object in other Thread Thread 2 Thread 1 Use of s. s = new String() An implementation detail. The java.lang.String has private members char value[] int offset int count int hash
  • 9. Dereferenced Object in other Thread • Broken JMM in JDK 1.4 and earlier. • The immutable object (of java.lang.String as a real example) has a proper visibility of the values in internal fields into other threads due to fixes in JMM since JDK 1.5.
  • 10. Combined Fields on an Instance What must be the values. Thread 2 Thread 1 provider = new FinalFieldExample(); <read> provider.f; class FinalFieldExample { <read> provider.x; private final Object f; private Object x; FinalFieldExample() { x = new Object(); f = new Object(); } }
  • 11. Combined Fields on an Instance What must be the values. • Suppose<provider> is non-final field, non-null. • The value which is set to <x> in the constructor has improper visibility in Thread 2. • Value assigned to <f> is properly visible to Thread 2. • Dereference and Memory Chains for the read of <f> in Thread 2 can pass through any reads by Thread 2 of a reference to <provider>.
  • 12. Combined Fields on an Instance What must be the values. Thread 2 Thread 1 provider = new FinalFieldExample(); <read> provider.f; class FinalFieldExample { <read> provider.g; private final Object f; <read> provider.a; private final Object g; <read> provider.b; private Object a, b, c, d; <read> provider.c; <read> provider.d; FinalFieldExample() { a = new Object(); b = new Object(); f = new Object(); c = new Object(); g = new Object(); d = new Object(); } }
  • 13. Combined Fields on an Instance What must be the values. • Values in a, b, c, and d have no guarantee to be properly visible by a read in Thread 2. (see previous slide) • Since the <f> and <g> are final, their operations cannot be reordered with other. • Operations on <a>, <b> can be reordered. • <c> is always between <f> and <g>. • <d> stays behind <g>
  • 14. Reflection on Final Field What must be the value in f DO NOT USE IN CONCURRENT CODE private final Object f = …; getClass().getField("f").set(this, new Object()); return f;
  • 15. Reflection on Final Field What must be the value in f • If a final field is initialized to a compile-time constant in the field declaration, changes to the final field may not be observed, since uses of that final field are replaced at compile time with the compile-time constant.
  • 16. Aggressive optimization of final fields. • Within a thread, it is permissible to reorder reads of a final field with calls to methods that may change final fields via reflection. • Reflection can be used to change final fields after the constructor for the object completes.
  • 17. Aggressive optimization of final fields. • A method which only returns a value held by final field will be inlined with compile-time value into method’s caller code; • Static methods and private instance methods are considered final and inlined; • Method code smaller than 35 bytes in byte code used to be inlined (-XX:MaxInlineSize) and therefore a subject of further reordering.