Skip to content

Instantly share code, notes, and snippets.

@ThinTiger
Forked from JeffreyZhao/gist:3957082
Created October 26, 2012 10:42
Show Gist options
  • Save ThinTiger/3958128 to your computer and use it in GitHub Desktop.
Save ThinTiger/3958128 to your computer and use it in GitHub Desktop.
// The code below would print overlapped A and B sequences like:
// ...
// A
// A
// B
// B
// ...
//
// Please add multi-threading protection code to make sure that
// As and Bs are printed in the order of:
// ...
// A
// B
// A
// B
// ...
private readonly static object lockerA = new object();
private readonly static object lockerB = new object();
static void PrintA()
{
lock(lockerA)
{
Console.WriteLine("A");
}
}
static void PrintB()
{
lock(lockerB)
{
Console.WriteLine("B");
}
}
// DO NOT touch the code below
for (var i = 0; i < 10; i++)
{
new Thread(() => {
while (true) {
PrintA();
PrintB();
}
}).Start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment