Using the same lock for multiple methods

前端 未结 5 1635
时光说笑
时光说笑 2021-01-01 13:07

I haven\'t had any issues using the same lock for multiple methods so far, but I\'m wondering if the following code might actually have issues (performance?) that I\'m not a

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-01 14:05

    Shared lock locks other non-related calls

    If you use the same lock then locking in one method unnecessarily locks others as well. If they're not related at all than this is a problem since they have to wait for each other. Which they shouldn't.

    Bottleneck

    This may pose a bottleneck when these methods are frequently called. With separate locks they would run independently, but sharing the same lock it means they must wait for the lock to be released more often as required (actually three times more often).

提交回复
热议问题