title: C# Multiple Locks on list of objects, will I avoid a deadlock?
body: <p>I have a list of 1,000 objects, each of which contain their own LockObject.</p>
  
  <p>I plan on spinning up multiple threads running the same thing. They will pick 6 random objects from the list of 1,000, and then lock on their lock objects. Do something with them before releasing them.</p>
  
  <p>When I create the list of 6 objects I want to work with that cycle, assuming I order the list by some unique value within each object, would the following avoid a deadlock situation between 2 or more such threads?</p>
  
  <pre><code>Players = Players.OrderBy(P =&gt; P.ID).ToList();
  
  Lock(Players[0].LockObj)
      Lock(Players[1].LockObj)
          Lock(Players[2].LockObj)
              Lock(Players[3].LockObj)
                  Lock(Players[4].LockObj)
                      Lock(Players[5].LockObj)
                      {
                          //...
                      }
  </code></pre>
  
  <p>I was reading up on this to try and figure out if this was the "correct" way to write this, it appears that it is, but people gave warning about deadlocks. I believe that this would avoid deadlocks but am not sure 100%.</p>