Re: static initialization and thread safety (Bobrowski, Maciej)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Re: static initialization and thread safety (Bobrowski, Maciej)

Valentin Kovalenko
The short answer is yes.

This is described in JLS 12.4.2. Detailed Initialization Procedure (, and expressed via the initialization lock LC. There is also the following note regarding possible optimizations of initialization procedure: "An implementation may optimize this procedure by eliding the lock acquisition in step 1 (and release in step 4/5) when it can determine that the initialization of the class has already completed, provided that, in terms of the memory model, all happens-before orderings that would exist if the lock were acquired, still exist when the optimization is performed."

Date: Tue, 4 Apr 2017 14:49:34 +0000
From: "Bobrowski, Maciej" <[hidden email]>
To: "[hidden email]"
        <[hidden email]>
Subject: [concurrency-interest] static initialization and thread
        <[hidden email]>
Content-Type: text/plain; charset="utf-8"

I was wondering what guarantees there are about non-final fields initialized as part of class initialization. Let's consider

Class Point{

    int x, y;

    public Point(int x, int y) {
         this.x = x;
         this.y = y;

Class Init{
    public static Point p = new Point(1,2)
    public static Point p2;

    static {
        p2 = new Point(3 , 4)
        p2.x = 10


If I have multiple threads trying to read p and p2, would ALL threads wait until those objects are fully constructed and assigned to corresponding fields (like in final fields case) due to initial;ization semantics, or is that not the case?



NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link:  If you cannot access these links, please notify us by reply message and we will send the contents to you. By communicating with Morgan Stanley you consent to the foregoing and to the voice recording of conversations with personnel of Morgan Stanley.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>


Subject: Digest Footer

Concurrency-interest mailing list
[hidden email]


End of Concurrency-interest Digest, Vol 147, Issue 1

Concurrency-interest mailing list
[hidden email]