What is thread contention?

后端 未结 10 876
[愿得一人]
[愿得一人] 2020-11-27 09:53

Can someone please explain simply what thread contention is?

I have googled it, but cannot seem to find a simple explanation.

相关标签:
10条回答
  • 2020-11-27 10:19

    From here:

    A contention occurs when a thread is waiting for a resource that is not readily available; it slows the execution of your code, but can clear up over time.

    A deadlock occurs when a thread is waiting for a resource that a second thread has locked, and the second thread is waiting for a resource that the first thread has locked. More than two threads can be involved in a deadlock. A deadlock never resolves itself. It often causes the whole application, or the part that is experiencing the deadlock, to halt.

    0 讨论(0)
  • 2020-11-27 10:23

    You have 2 threads. Thread A and Thread B, you also have object C.

    A is currently accessing object C, and has placed a lock on that object. B needs to access object C, but cannot do so until A releases the lock on object C.

    0 讨论(0)
  • 2020-11-27 10:23

    Another word might be concurrency. It is simply the idea of two or more threads trying to use the same resource.

    0 讨论(0)
  • 2020-11-27 10:24

    Imagine the following scenario. You are preparing for tomorrow's final examine and feel a little hungry. So, you give your younger brother ten bucks and ask him to buy a pizza for you. In this case, you are the main thread and your brother is a child thread. Once your order is given, both you and your brother are doing their job concurrently (i.e., studying and buying a pizza). Now, we have two cases to consider. First, your brother brings your pizza back and terminates while you are studying. In this case, you can stop studying and enjoy the pizza. Second, you finish your study early and sleep (i.e., your assigned job for today - study for tomorrow's final exam - is done) before the pizza is available. Of course, you cannot sleep; otherwise, you won't have a chance to eat the pizza. What you are going to do is to wait until your brother brings the pizza back.

    As in the example, the two cases give meaning of rivalry.

    0 讨论(0)
  • 2020-11-27 10:24

    Lock contention takes place when a thread tries to acquire the lock to an object which is already acquired by other thread*. Until the object is released, the thread is blocked (in other words, it is in the Waiting state). In some cases, this may lead to a so-called serial execution which negatively affects application.

    from dotTrace documentation

    0 讨论(0)
  • 2020-11-27 10:25

    Essentially thread contention is a condition where one thread is waiting for a lock/object that is currently being held by another thread. Therefore, this waiting thread cannot use that object until the other thread has unlocked that particular object.

    0 讨论(0)
提交回复
热议问题