static initialization and thread safety

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

static initialization and thread safety

Bobrowski, Maciej

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?

 

Thanks!

Maciej





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: http://www.morganstanley.com/disclaimers  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.


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: static initialization and thread safety

Martin Buchholz-3

On Tue, Apr 4, 2017 at 7:49 AM, Bobrowski, Maciej <[hidden email]> wrote:

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?

 

Thanks!

Maciej





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: http://www.morganstanley.com/disclaimers  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.


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest



_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: static initialization and thread safety

Alex Otenko
Needs to be added that due to lazy class initialization it is possible to incur an unintended deadlock, or observe uninitialized values, subject to cross-dependencies. Cross-dependencies are a bad thing, but you cannot always avoid them, or control - due to lazy class initialization.

Alex

On 4 Apr 2017, at 17:08, Martin Buchholz <[hidden email]> wrote:


On Tue, Apr 4, 2017 at 7:49 AM, Bobrowski, Maciej <[hidden email]> wrote:

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?

 

Thanks!

Maciej





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: http://www.morganstanley.com/disclaimers  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.






_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Loading...