Stories from the Code Reviews: Threading
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.
Comments
That first method seems wrong to me. Since that's an instance method, each thread calling that method would be locking on a new object instance. Thus you have no concurrency protection.
Or was the point of this post that that method is wrong? :)
Yes, this method is VERY wrong.
I updated the post to make it clear
Whats wrong with green on the screen?
Is this for real? You've got to be kidding.
@Juice,
Consider the affect of hundreds of lines, commented.
It may sooth some, but it annoys me.
Those are for real. Both of them.
The first one existed for about 15 minutes before I was called and fixed it, the second one... well, the second one was a production issue.
You should better post this on http://thedailywtf.com/ to not confuse your readers so much :)
The turn around of the WTF isn't very good, only one/two WTF per day.
I have a feeling that I can produce WTFs in commercial quanitites
Then we should team up, I can serve tons of them too. On the other hand, its the quality (in a negative sense), not the quantity that makes a WTF worth reading it.
Hehe ;-D
I don't think that I run into the DailyWTF worthy stuff every day, but I see a lot of smaller one, and the second example above is certainly something that can be proudly featured on the DailyWTF
Comment preview