use the same lock object at two different code block?

后端 未结 3 1067
温柔的废话
温柔的废话 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:30

    If you want to prevent different threads from performing task1 and task2 at the same time, then you must use the same lock object.

    If the two tasks do not contend for the same resources, you could use different lock objects.

提交回复
热议问题