Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Final field semantics

3,088 views

Published on

Final keyword in java is known to forbid class extension and modification of the fields. It is less known to have special meaning in multithreaded code.

Unfortunately, there is not that much information on the latter, and even most thorough talks avoid deep details on the beauty of finals.

In this talk I apply section 17.5 of java language specification to different examples and show how the spec works. Several myths are busted on the way.

Here's nice article on different aspects of JMM: http://shipilev.net/blog/2014/jmm-pragmatics/

  • I've got a question to slide 93 - Reflection in action (5). How can it be 0 as once T is initialized and its fileds frozen and its assingled to (GLOBAL = t) it should have at lest correct reference to U. Later when we replace t.fu via relection to new already constructed object then until that object is red by another thread before setting w.x then we shouldn't observe 0. We however could observe 0 if we first chane refference with a new object by reflection and then assingn w.x (so reorded two last lines in first column)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Great explanation!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • The best explanation of java final semantics. Thank you so much...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Final field semantics

  1. 1. Semantics of final fields in java Vladimir Sitnikov, Valentin Kovalenko sitnikov@netcracker.com, @VladimirSitnikv NetCracker September 2014
  2. 2. Introduction Examples 2 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  3. 3. Why final is required in JMM? 3 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  4. 4. String safety String s = ... if ( checkAccess (s)) { return readFile (s); } Is this a valid security check? 4 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  5. 5. String unsafety in 1.4 String s = ... if ( checkAccess (s)) { return readFile (s); } The answer depends on the java version, and in java 1.4 the code is insecure 5 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  6. 6. String unsafety in 1.4 String s = GLOBAL ; if ( checkAccess (s)) { return readFile (s); } HackThread GLOBAL = "/tmp/ etc / passwd " . substring (4); For instance: HackThread executes .substring(4) and transfers it via data race to the checker thread 6 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  7. 7. String unsafety in 1.4 String s = GLOBAL ; if ( checkAccess (s)) { return readFile (s); } HackThread GLOBAL = "/tmp/ etc / passwd " . substring (4); In java 1.4 result of substring references the same char array, and the value depends on String#offset and String#size 7 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  8. 8. String unsafety in 1.4 String s = GLOBAL ; if ( checkAccess (s)) { return readFile (s); } HackThread GLOBAL = "/tmp/ etc / passwd " . substring (4); race race Since no synchronization is in place, reader might observe not-fully-initialized String 8 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  9. 9. String unsafety in 1.4 String s = GLOBAL ; if ( checkAccess (s)) { return readFile (s); } HackThread GLOBAL = "/tmp/ etc / passwd " . substring (4); race race checkAccess might observe "/tmp/etc/passwd", and even then readFile might observe "/etc/passwd" 9 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  10. 10. String unsafety in 1.4 String s = GLOBAL ; if ( checkAccess (s)) { return readFile (s); } HackThread GLOBAL = "/tmp/ etc / passwd " . substring (4); race race checkAccess might observe "/tmp/etc/passwd", and even then readFile might observe "/etc/passwd" Even synchronization on s and volatile will not help! 10 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  11. 11. String safety in java 1.5+ String s = GLOBAL ; if ( checkAccess (s)) { return readFile (s); } HackThread GLOBAL = "/tmp/ etc / passwd " . substring (4); hb hb In java 1.5+ final protects from such non-initialized objects from HackThread 11 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  12. 12. Çà÷åì íàì JMM? 12 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  13. 13. Quiz int x = 1; public int neverTryThisAtHome () { int i = this .x; // it is 1, isn ’t it? this . setX (2); // just updates x to 2 return this .x - i; // 2 - 1 == ...? } What is the result? 1? 0? -1? 13 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  14. 14. Quiz int x = 1; public int neverTryThisAtHome () { int i = this .x; // it is 1, isn ’t it? this . setX (2); // just updates x to 2 return this .x - i; // 2 - 1 == ...? } OK, the result is 1 14 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  15. 15. Quiz final int x = 1; public int neverTryThisAtHome () { int i = this .x; // it is 1, isn ’t it? this . setX (2); // just updates x to 2 return this .x - i; // 2 - 1 == ...? } Let’s add some final 15 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  16. 16. Quiz final int x = 1; public int neverTryThisAtHome () { int i = this .x; // it is 1, isn ’t it? this . setX (2); // just updates x to 2 return this .x - i; // 2 - 1 == ...? } The specification allows all the cases: 1, 0, and even -1! (see also example 17.5.3-1) 16 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  17. 17. A bit of theory 17 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  18. 18. Program order I Program order is a total order among inter-thread actions of each thread in source code order 18 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  19. 19. Program order I Program order is a total order among inter-thread actions of each thread in source code order I Compiler is forbidden to reorder/alter/ignore operations if observable behavior violates program order 19 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  20. 20. Program order I Program order is a total order among inter-thread actions of each thread in source code order I Compiler is forbidden to reorder/alter/ignore operations if observable behavior violates program order I It does not mean the program is executed in program order 20 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  21. 21. Program order I Program order is a total order among inter-thread actions of each thread in source code order I Compiler is forbidden to reorder/alter/ignore operations if observable behavior violates program order I It does not mean the program is executed in program order I For instance: program order is not defined for operations on local variables 21 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  22. 22. Partial order I In section 17 JLS "partial order" is mentioned 8 times 22 / 104 (c) Copyright 2014, NetCracker Technology Corp. All rights reserved
  23. 23. Partial order I In section 17 JLS "partial order" is mentioned 8 times I Partial order is hb

×