1

Topic: How also is lazy to initialize value?

The class was necessary for me, I wrote it: class LazyReference <T> {private T value; private Supplier <T> supplier; public LazyReference (Supplier <T> supplier) {this.supplier = supplier;} public T get () {if (value == null) {synchronized (this) {if (value == null) {value = Objects.requireNonNull (supplier.get ()); supplier = null;}}} return value;} } And though I do not understand, why it is wrong, on SO all repeat that it is wrong: https://stackoverflow.com/questions/298 … t-volatile I, to tell the truth, all the same think that they are wrong, because I  the decision without volatile a heap of flows and any problems do not see, on Intel at least. Like as at Intel more strict warranties. Can on ARM any and falls down, certainly... The Same synchronized should publish all in the correct order, differently what for it is necessary. With all to argue difficult even if you are right. Well it is fine. volatile to thrust no wonder. But the overhead projector turns out and it is not pleasant to me. Like as it is possible to use a class with a final-field and like as it something there safely publishes after the designer without everyones volatile. That is we replace T value on ValueHolder <T> value. But how much it is safe, not absolutely clearly. Is still any AtomicReference, like too from this opera. But there  the same volatile. And  shows that many face this task. And I not for the first time faced. And where, actually, to take ready,  the correct and megafast decision? For certain any guru through Unsafe-chegoto writes most abruptly without sacrificing productivity or a correctness. In JDK did not find, in guava too.

2

Re: How also is lazy to initialize value?

Hello, vsb, you wrote: vsb> In JDK did not find, in guava too. https://google.github.io/guava/releases … e.Supplier -

3

Re: How also is lazy to initialize value?

Hello, halo, you wrote: vsb>> In JDK did not find, in guava too. H> https://google.github.io/guava/releases … e.Supplier - Thanks, looked through. Only implementation does not please: class NonSerializableMemoizingSupplier <T> implements Supplier <T> {volatile Supplier <T> delegate; volatile boolean initialized;//"value" does not need to be volatile; visibility piggy-backs//on volatile read of "initialized". @NullableDecl T value; NonSerializableMemoizingSupplier (Supplier <T> delegate) {this.delegate = Preconditions.checkNotNull (delegate);} @Override public T get () {//A 2-field variant of Double Checked Locking. if (! initialized) {synchronized (this) {if (! initialized) {T t = delegate.get (); value = t; initialized = true;//Release the delegate to GC. delegate = null; return t;}}} return value;}} the same volatile under a cowl. Really it is impossible without it? Also the delegate does not release, if it . This implementation is not pleasant to me.

4

Re: How also is lazy to initialize value?

Hello, vsb, you wrote: vsb> vsb> class LazyReference <T> {vsb> private T value; vsb> private Supplier <T> supplier; vsb> public LazyReference (Supplier <T> supplier) {vsb> this.supplier = supplier; vsb>} vsb> public T get () {vsb> if (value == null) {vsb> synchronized (this) {vsb> if (value == null) {vsb> value = Objects.requireNonNull (supplier.get ()); vsb> supplier = null; vsb>} vsb>} vsb>} vsb> return value; vsb>} vsb>} vsb> vsb> And though I do not understand why it is wrong, on SO all repeat that it is wrong: https://stackoverflow.com/questions/298 … t-volatile Listen, there Shipilev wrote. With such people do not argue It certainly a little without adducing any proof, but it is necessary to esteem nevertheless,  why  "double checked locking without volatile" does not work. Here on-essence the same problem. supplier.get With all requireNonNull it is easy can , null check generally will be thrown out and there will be only a call of the designer and storage selection under object. vsb> I, to tell the truth, all the same think that they are wrong, because I  the decision without volatile a heap of flows and any problems do not see, on Intel at least. Like as at Intel more strict warranties. Can on ARM any and falls down, certainly... The Same synchronized should publish all in the correct order, differently what for it is necessary. The problem that can be published before synchronized ends. The specification of it does not forbid. vsb> with all to argue difficult even if you are right. It  a position. The specification can be right only. vsb> well it is fine. volatile to thrust no wonder. But the overhead projector turns out and it is not pleasant to me. You can measure it? It is really important? vsb> Like as it is possible to use a class with a final-field and like as it something there safely publishes after the designer without everyones volatile. That is we replace T value on ValueHolder <T> value. But how much it is safe, not absolutely clearly. Is still any AtomicReference, like too from this opera. But there  the same volatile. final will just work. For final there are warranties of that the object will be completely created before registers in this field. AtomicReference has CAS an overhead projector. At small  it will be not big if at all will be. By the way, if simply to hang up lock in the field the overhead projector, probably, will be about same (there too CAS) if  it is not a lot of. vsb> And  shows that many face this task. And I not for the first time faced. And where, actually, to take ready,  the correct and megafast decision? For certain any guru through Unsafe-chegoto writes most abruptly without sacrificing productivity or a correctness. In JDK did not find, in guava too. So, there and then the answer was: https://stackoverflow.com/questions/298 … t-volatile c final a field. Still  it is simple not . It is very difficult to prove that volatile something there .

5

Re: How also is lazy to initialize value?

Hello, StanislavK, you wrote: vsb>> I, to tell the truth, all the same think that they are wrong, because I  the decision without volatile a heap of flows and any problems do not see, on Intel at least. Like as at Intel more strict warranties. Can on ARM any and falls down, certainly... The Same synchronized should publish all in the correct order, differently what for it is necessary. SK> the problem that can be published before synchronized ends. The specification of it does not forbid. Well here it also is amazing for me. In my understanding synchronized it is a heavy construction which everything that only is possible, should synchronize as it is necessary with all barriers of storage and . And here any designer with final-fields which generally to synchronization, generally speaking, has no relation, interposes barriers, and synchronized is not present. The strange situation. Perhaps, certainly, so historically it turned out. vsb>> well it is fine. volatile to thrust no wonder. But the overhead projector turns out and it is not pleasant to me. SK> you can measure it? It is really important? Yet did not measure, but it is obvious that it is, differently what for volatile is necessary. vsb>> like as it is possible to use a class with a final-field and like as it something there safely publishes after the designer without everyones volatile. That is we replace T value on ValueHolder <T> value. But how much it is safe, not absolutely clearly. Is still any AtomicReference, like too from this opera. But there  the same volatile. SK> final will just work. For final there are warranties of that the object will be completely created before registers in this field. Apparently it also will be the fastest decision, it is strange that in Guava so did not make (the post above) see. SK> So, there and then the answer was: https://stackoverflow.com/questions/298 … t-volatile c final a field. Well I there did not find the library decision. In  is, prompted, though this decision is not pleasant to me, any it strange, is visible  I will make the when hands reach. SK> still  it is simple not . It is very difficult to prove that volatile something there . Well it is an interest question, more likely. The specific code can be and slow, but the code claiming on "" should be fast, . Can be JVM clever and removes volatile after initialization if sees, what the variable appropriates value once?

6

Re: How also is lazy to initialize value?

Hello, vsb, you wrote: SK>> the Problem that can be published before synchronized ends. The specification of it does not forbid. vsb> well here it also is amazing for me. In my understanding synchronized it is a heavy construction which everything that only is possible, should synchronize as it is necessary with all barriers of storage and . And here any designer with final-fields which generally to synchronization, generally speaking, has no relation, interposes barriers, and synchronized is not present. The strange situation. Perhaps, certainly, so historically it turned out. There is an accurate specification as all it works: https://docs.oracle.com/javase/specs/jl … ls-17.html Briefly,  only that all that does one flow inside synchronized precisely sees other flow which came in synchronized with the same monitor after the first flow. About final there too is (17.5). Barriers it as this specification  on  i.e. as they there are precisely interposed knows only the compiler. It seems to me that if not to go down on level of development of compilers about barriers  not to recall and operate with concept happens-before (17.4.5). SK>> you can measure it? It is really important? vsb> yet did not measure, but it is obvious that it is, differently what for volatile is necessary. Is is. But if it is not important, what for ? SK>> final will just work. For final there are warranties of that the object will be completely created before registers in this field. vsb> apparently it also will be the fastest decision, it is strange that in Guava so did not make (the post above) see. Probably did not guess. SK>> so, there and then the answer was: https://stackoverflow.com/questions/298 … t-volatile c final a field. vsb> well I there did not find the library decision. In  is, prompted, though this decision is not pleasant to me, any it strange, is visible  I will make the when hands reach. It seems to me that did not make mostly because it is not necessary. And those it is really necessary for a clod normally enough  that it to make correctly and quickly. SK>> still  it is simple not . It is very difficult to prove that volatile something there . vsb> Well it is an interest question, more likely. The specific code can be and slow, but the code claiming on "" should be fast, . Can be JVM clever and removes volatile after initialization if sees, what the variable appropriates value once? JIT  is able to do it, it does not break the specification.

7

Re: How also is lazy to initialize value?

vsb> In my understanding synchronized it is a heavy construction, synchronized very fast construction (to deliver ID  in title of object and all). It simply it is not so good to lay down on parallelism and competition because forces all flows to wait for clearing . If to this method from different flows simultaneously address rarely it will be  all.

8

Re: How also is lazy to initialize value?

Hello, bzig, you wrote: B> synchronized very fast construction (to deliver ID  in title of object and all). It simply it is not so good to lay down on parallelism and competition because forces all flows to wait for clearing . If to this method from different flows simultaneously address rarely it will be  all. synchronized slow, but in this case under synchronized come only once, and in the core instead check on null volatile a variable that is essential faster.

9

Re: How also is lazy to initialize value?

T> synchronized slow, but in this case under synchronized come only once, and in the core instead check on null volatile a variable that is essential faster. Essentially is how many?

10

Re: How also is lazy to initialize value?

Hello, bzig, you wrote: T>> synchronized slow, but in this case under synchronized come only once, and in the core instead check on null volatile a variable that is essential faster. B> essentially is how many? And so simply you will not tell. Inside synchronized there can be only one flow, remaining wait. If for them waits much, any will long stand. If them 2 or 3, a pattern already another. If 2 or 3 continuously hollow critical , the third

11

Re: How also is lazy to initialize value?

Hello, Terix, you wrote: T> Hello, bzig, you wrote: T>>> synchronized slow, but in this case under synchronized come only once, and in the core instead check on null volatile a variable that is essential faster. B>> essentially is how many? T> And so simply you will not tell. Inside synchronized there can be only one flow, remaining wait. If for them waits much, any will long stand. If them 2 or 3, a pattern already another. If 2 or 3 continuously hollow critical  the third Well here I am equal about it and wrote - if probability of simultaneous access low, in itself  very fast.

12

Re: How also is lazy to initialize value?