static void Main(string[] args)
{
Thread thread = new Thread(WriteY);
thread.Name = "Y Thread ...";
thread.Start();
for (int i = 0; i < 1000; i++)
{
System.Console.Write("x");
}
}
static void WriteY()
{
for (int i = 0; i < 1000; i++)
{
System.Console.Write("y");
}
}
// 程序运行结果
// xxxxxxxxxxxyyyxxxxxx...xxxyyyxxxxxxxxxxyyyxxxxxx
线程的执行与另外一个线程上代码的执行交织的那一点
// 针对Thread.CurrentThread 的例子
static void Main(string[] args)
{
Thread.CurrentThread.Name = "Main Thread ...";
Thread thread = new Thread(WriteY);
thread.Name = "Y Thread ...";
thread.Start();
System.Console.Write(Thread.CurrentThread.Name);
for (int i = 0; i < 1000; i++)
{
System.Console.Write("x");
}
}
static void WriteY()
{
System.Console.Write(Thread.CurrentThread.Name);
for (int i = 0; i < 1000; i++)
{
System.Console.Write("y");
}
}
// Join 简单例子
static void Main(string[] args)
{
Thread t1 = new Thread(Go);
t1.Start();
t1.Join();
System.Console.WriteLine("Thread t1 has ended");
}
static void Go()
{
for (int i = 0; i < 1000; i++)
{
Console.Write("Y");
}
}
// 程序运行结果
// YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
// ...
// YYYYYYYYYYYYYYYYYYYYYYYYYYYYYThread t1 has ended
// Join 复杂例子
static Thread thread1, thread2;
static void Main(string[] args)
{
thread1 = new Thread(ThreadProc);
thread1.Name = "thread1";
thread1.Start();
thread2 = new Thread(ThreadProc);
thread2.Name = "thread2";
thread2.Start();
}
static void ThreadProc()
{
System.Console.WriteLine("\nCurrent Thread {0}", Thread.CurrentThread.Name);
System.Console.WriteLine("Thread2 State : {0}", thread2.ThreadState);
if (Thread.CurrentThread.Name == "thread1" &&
thread2.ThreadState != ThreadState.Unstarted)
{
thread2.Join();
}
// Thread.Sleep(2000);
System.Console.WriteLine("\nCurrent Thread : {0}", Thread.CurrentThread.Name);
System.Console.WriteLine("Thread1 State : {0}", thread1.ThreadState);
System.Console.WriteLine("Thread2 State : {0}", thread2.ThreadState);
}
// 程序运行结果
// Current Thread thread1
// Thread2 State : WaitSleepJoin
// Current Thread thread2
// Thread2 State : Running
// Current Thread : thread2
// Thread1 State : WaitSleepJoin
// Thread2 State : Running
// Current Thread : thread1
// Thread1 State : Running
// Thread2 State : Stopped
注意⚠️:
// Join 超时 毫秒例子
static Thread thread1, thread2;
static void Main(string[] args)
{
thread1 = new Thread(ThreadProc);
thread1.Name = "thread1";
thread1.Start();
thread2 = new Thread(ThreadProc);
thread2.Name = "thread2";
thread2.Start();
}
static void ThreadProc()
{
System.Console.WriteLine("\nCurrent Thread {0}", Thread.CurrentThread.Name);
System.Console.WriteLine("Thread2 State : {0}", thread2.ThreadState);
if (Thread.CurrentThread.Name == "thread1" &&
thread2.ThreadState != ThreadState.Unstarted)
{
if (thread2.Join(2000))
{
System.Console.WriteLine("Thread2 has termminated.");
}
else
{
System.Console.WriteLine("The timeout has elapsed and Thread1 will resume.");
}
}
System.Console.WriteLine("\nCurrent Thread : {0}", Thread.CurrentThread.Name);
System.Console.WriteLine("Thread1 State : {0}", thread1.ThreadState);
System.Console.WriteLine("Thread2 State : {0}", thread2.ThreadState);
}
// 程序运行结果
// Current Thread thread2
// Current Thread thread1
// Thread2 State : WaitSleepJoin
// Thread2 State : Running
// Current Thread : thread2
// Thread1 State : WaitSleepJoin
// Thread2 State : Running
// Thread2 has termminated.
// Current Thread : thread1
// Thread1 State : Running
// Thread2 State : Stopped
// Join 超时 TimeSpan 例子
static TimeSpan waitTime = new TimeSpan(0, 0, 1);
static void Main(string[] args)
{
Thread newThread = new Thread(Work);
newThread.Start();
if (newThread.Join(waitTime - waitTime))
{
System.Console.WriteLine("New thread terminated");
}
else
{
System.Console.WriteLine("Join timed out.");
}
}
static void Work()
{
Thread.Sleep(waitTime);
}
// 输出结果
// Join timed out.
bool blocked = (someThread.ThreadState & ThreadState.WaitSleepJoin) != 0;
while (DateTime.Now < nexeStartTime)
Thread.Sleep(100);
while (DateTime.Now < nexeStartTime);
// 线程不安全的例子
static bool _done = false;
static void Main(string[] args)
{
new Thread(Go).Start();
Go();
}
static void Go()
{
if (!_done)
{
System.Console.WriteLine("Done");
Thread.Sleep(50);
_done = true;
}
}
// 输出结果
// Done
// Done