Why did Java and C# add intrinsic lock to every object?

前端 未结 4 637
悲&欢浪女
悲&欢浪女 2020-12-31 05:06

Making every object lockable looks like a design mistake:

  1. You add extra cost for every object created, even though you\'ll actually use it only in a tiny fract
4条回答
  •  独厮守ぢ
    2020-12-31 05:48

    Actually you only have reference to that monitor in each object; the real monitor object is created only when you use synchronization => not so much memory is lost.

    The alternative would be to add manually monitor to those classes that you need; this would complicate the code very much and would be more error-prone. Java has traded performance for productivity.

提交回复
热议问题