Thread Confinement

后端 未结 8 686
生来不讨喜
生来不讨喜 2020-12-02 14:43

I am reading Java Concurrency in Practice and kind of confused with the thread confinement concept. The book says that

When an object is confined to a

8条回答
  •  难免孤独
    2020-12-02 14:47

    So when an object is confined to a thread, no other thread can have access to it?

    That's what thread confinement means - the object can only EVER be accessed by one thread.

    Is that what it means to be confined to a thread?

    See above.

    How does one keep an object confined to a thread?

    The general principle is to not put the reference somewhere that would allow another thread to see it. It is a little bit complicated to enumerate a set of rules that will ensure this, but (for instance) if

    • you create a new object, and
    • you never assign the object's reference to an instance or class variable, and
    • you never call a method that does this for the reference,
    • then the object will be thread confined.

提交回复
热议问题