Created
October 26, 2012 05:43
-
-
Save JeffreyZhao/3957082 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
// ... | |
static void PrintA() | |
{ | |
Console.WriteLine("A"); | |
} | |
static void PrintB() | |
{ | |
Console.WriteLine("B"); | |
} | |
// DO NOT touch the code below | |
for (var i = 0; i < 10; i++) | |
{ | |
new Thread(() => { | |
while (true) { | |
PrintA(); | |
PrintB(); | |
} | |
}).Start(); | |
} |
private static readonly object LockObj = new object();
static void PrintA()
{
Monitor.Enter(LockObj);
Console.WriteLine("A");
}
static void PrintB()
{
Console.WriteLine("B");
Monitor.Exit(LockObj);
}
static object locka = new object();
static object lockb = new object();
static bool result = true;
static void PrintA()
{
if (result == true)
{
lock (locka)
{
if (result == true)
{
Console.Write("A");
result = false;
}
}
}
}
static void PrintB()
{
if (result == false)
{
lock (lockb)
{
if (result == false)
{
Console.Write("B");
result = true;
}
}
}
}
class MainClass
{
static readonly object obj = new object();
static void PrintA()
{
Monitor.Enter(obj);
Console.WriteLine("A");
}
static void PrintB()
{
Console.WriteLine("B");
Monitor.Exit(obj);
}
public static void Main (string[] args)
{
// DO NOT touch the code below
for (var i = 0; i < 10; i++)
{
new Thread(() => {
while (true) {
PrintA();
PrintB();
}
}).Start();
}
}
}
多打印几个,最后一个字符换行,比较容易看出效果
class Program
{
static object o = new object();
static void Main(string[] args)
{
// DO NOT touch the code below
for (var i = 0; i < 10; i++)
{
new Thread(() =>
{
while (true)
{
lock (o)
{
PrintA();
PrintB();
PrintC();
PrintD();
PrintE();
}
}
}).Start();
}
Console.Read();
}
static void PrintA()
{
Console.Write("A");
}
static void PrintB()
{
Console.Write("B");
}
static void PrintC()
{
Console.Write("C");
}
static void PrintD()
{
Console.Write("D");
}
static void PrintE()
{
Console.WriteLine("E");
}
}
private static Semaphore semA = new Semaphore(1,1);
private static Semaphore semB = new Semaphore(0,1);
static void PrintA()
{
semA.WaitOne();
Console.WriteLine("A");
semB.Release();
}
static void PrintB()
{
semB.WaitOne();
Console.WriteLine("B");
semA.Release();
}
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();
}
//俩ThreadStart 把俩方法委托进去
ThreadStart tsA = new ThreadStart(PrintA);
ThreadStart tsB = new ThreadStart(PrintB);
//俩委托调用. acB = null 是因为不赋值后面报未赋值错误.
AsyncCallback acA, acB = null;
//PrintA结束委托后,再调用tsB
acA = new AsyncCallback(delegate(IAsyncResult r)
{
tsA.EndInvoke(r);
Thread.Sleep(37);
tsB.BeginInvoke(acB, null);
});
//PrintB结束委托后,再调用tsA,写成个套套.
acB = new AsyncCallback(delegate(IAsyncResult r)
{
tsB.EndInvoke(r);
Thread.Sleep(51);
tsA.BeginInvoke(acA, null);
});
//然后就开始死磕
tsA.BeginInvoke(acA, null);
//以下是干扰代码 用于测试
new Thread(delegate() {
while (true)
{
Thread.Sleep(521);
Console.Write("-");
}
}).Start();
//主线程的干扰
while (true)
{
Thread.Sleep(795);
Console.Write("=");
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这样行不行?
using System;
using System.Threading;
class Program
{
static void PrintA()
{
Console.WriteLine("A");
}
}