How to simulate constructor race conditions?

后端 未结 4 659
故里飘歌
故里飘歌 2020-12-19 23:03

I am reading \"Java Concurrency in practice\" and looking at the example code on page 51.

This states that if a thread has references to a shared object then other t

4条回答
  •  不思量自难忘°
    2020-12-19 23:28

    This will never throw a RunTimeException because your Widgit instance variable w remains null until the constructor code has executed. While your main thread is sleeping in the Widgit constructor, your Checker instance is hitting NullPointerException constantly as the w variable is still null. When your main thread finishes construction, the two int variables in Widgit are equal.

提交回复
热议问题