use the same lock object at two different code block?

后端 未结 3 1172
温柔的废话
温柔的废话 2020-12-18 18:36

Can I use the same lock object at two methods, accessed by two different threads? Goal is to make task1 and task2 thread safe.

object lockObject = new object         


        
3条回答
  •  被撕碎了的回忆
    2020-12-18 19:42

    Yes.

    You can and it works. If you don't use the same object, the blocks could execute at the same time. If you do use the same object, they can't.

    Also, you mean lock(lockObject), not using(lockObject).

提交回复
热议问题