« Return to Thread: Parametric Initialization On DemandHolder Idiom ?

Re: Parametric Initialization On DemandHolder Idiom ?

by Hanson Char :: Rate this Message:

Reply to Author | View in Thread

On 9/5/07, Beverly Sanders <sanders@...> wrote:

> In this example, safe publication is needed (it is safe when
> goodHolder is volatile)
>
> class FinalHolder {
>   static [volatile] FinalHolder goodHolder;
>
> final int f;
> FinalHolder() {
>   f = 1;
> }
> void printFinal() {
> System.out.println(f);
> }
>
> Thread1:
> Thread2.start();
> goodHolder = new FinalHolder();
>
> Thread2:
> while(goodHolder==null){}
>            //termination guaranteed only if goodHolder volatile

True.

> goodHolder.print();
>            //if thread gets here, it will print 1 due to
>            //correct construction (no leak of ref from consructor)
>            //and final field semantics.
>            //But if goodHolder volatile, f doesn't need to be
>            //final except to ensure that the value is not modified--special
>            //treatment in the memory model is not needed.

True also.

Hanson Char
_______________________________________________
Concurrency-interest mailing list
Concurrency-interest@...
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest

 « Return to Thread: Parametric Initialization On DemandHolder Idiom ?