[concurrency-interest] static initialization and thread safety

Alex Otenko oleksandr.otenko at gmail.com
Tue Apr 4 12:39:13 EDT 2017


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 <martinrb at google.com> wrote:
> 
> https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4.2 <https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.4.2>
> 
> On Tue, Apr 4, 2017 at 7:49 AM, Bobrowski, Maciej <Maciej.Bobrowski at morganstanley.com <mailto:Maciej.Bobrowski at morganstanley.com>> 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 <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
> Concurrency-interest at cs.oswego.edu <mailto:Concurrency-interest at cs.oswego.edu>
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest <http://cs.oswego.edu/mailman/listinfo/concurrency-interest>
> 
> 
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20170404/20c17372/attachment-0001.html>


More information about the Concurrency-interest mailing list