Stories from the Code Reviews: Threading

time to read 3 min | 545 words

I am doing code reviews, and I am running into some unique ideas about how code should work. Take into account this new locking pattern:

public void ShouldOnlyAllowSingleInstance()
{
  object syncLock = new object();
  lock(syncLock)
  {
    // code that modify a thread-shared resource
   }
}

Everyone know that you can use threads to increase the reponsiveness and throughput of your application, but this approach reach a whole another level, although it didn't manage to meet quite the scalability requirements

public void CopyData(IDbCommand cmd)
{
    cmd.CommandText = "SELECT * FROM TableWith_700_Rows;"
    IDataReader reader = cmd.ExecuteReader();
   while(reader.Read())
   {
     CustomerCopier customer = CustomerCopier.FromReader(reader);
    new Thread(customer.DoCopy).Start();
   }
}

// In CustomerCopier

public void DoCopy()
{
   using(SqlConnection connection = CreateConnectionToSourceDb())
  {
    using(ITransaction tx = connection.BeginTransaction(SeperationLevel.Serializable))
    {
      // execute insert
    }
  }
}

Oh, and here is a general comment, while green is a soothing color, I do not need to see so much of it on the screen.

Update: In case is wasn't clear, the above samples are really bad code.