Race Condition was solved now, everything is very good.
After N day passes, academic the demon book that you read a Refactoring accidentally, move deeply for it, him preparation tries this to reframe a few programs that had written before, found then above this paragraph of code. You had been the Java dish bird previously no longer, the method that knows very well Synchronized passes should compare not synchronous method to go up slow on speed 100 times, at the same time you also discover, when calling this method for the first time only, ability needs synchronism, and once Res initialization is finished, synchronism is done not have completely necessary. So you reframe code very quickly into the appearance below:
Code 3
Class Foo {Private Resource Res = Null; Public Resource GetResource() {If (res==Null){Synchronized(this){If(res==Null){Res = New Resource();}} } Return Res; }}
This kind looks very perfect optimizing skill is Double-Checked Locking. But very regretful, according to the language standard of Java, the code above is fluky.
One of causes that create DCL be no longer in force are to compile implement optimize the order that can adjust code. If be below circumstance of single line Cheng,carrying out a result only is correct, can think to compile implement the adjustment with such act on one's own “ the behavior of code order ” is lawful. The regulation of JLS in some way is freer, namely to let JVM more leeway undertakes code is optimized carry out efficiency in order to rise. And present CPU is used mostly exceed automation line technology to accelerate code to implement rate, be aimed at such CPU, compile implement the order that one of methods that adopted code optimizes are adjusting certain code namely, the instruction automation line that CPU does not allow when assuring to be carried out in the program as far as possible is broken stream, raise the executive rate of the program thereby. Be such code adjusts the invalidation that can bring about DCL. To prove this issue further, cite " DCL Broken Declaration " the example in the article:
Establish code of a Java:
Objects[i].reference = New Object();
Compile through Symantec JIT implement after been compile, final meeting becomes following collection codes to carry out in the machine:
0206106A Mov Eax, 0F97E78h0206106F Call 01F6B210; applies for return of value of memory space; to be put in the 02061074 Mov Dword Ptr [ebp] in Eax for Object, the address; that Objects[i].reference is in Eax; EBP puts returned dimensional address among them; right now Object have not initialization 02061077 Mov Ecx, the content; that place of Dword Ptr [eax] ; Dereference Eax points to is obtained new the initiative address 02061079 Mov Dword Ptr [ecx] that establishs a target, below 100h; 4 are inside the tectonic function 0206107F Mov Dword Ptr [ecx 4] of couplet, 200h 02061086 Mov Dword Ptr [ecx 8] , 400h0206108D Mov Dword Ptr [ecx 0Ch] , 0F84030h
Previous 1 2 34 5 6 7 8 Next