/*
* 測試線程的“死鎖”,及解決方法
*
*
*
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace A18_IndepthStudyMultitheading
{
class TestClass
{
private int _GameState = 100;
private object objLock = new object();
public void ChangeState()
{
while (true)
{
lock(objLock) //通過lock關鍵字讓多個線程順序訪問公共數據。
{
ChangeMyState();
}
}
}
private void ChangeMyState()
{
++_GameState;
if (_GameState == 100) //
{
Console.WriteLine("_GameState == 100,本語句一般情況下不會輸出");
}
_GameState = 100; //多線程在爭搶資源時有可能會因爲這條語句而得到_GameState的值爲100,從而使上面的判斷成立
}
}
}
/*
* 進一步演示“死鎖”示例
*
*
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace A18_IndepthStudyMultitheading
{
class TestClass2
{
private int _Num = 0;
private object objLock = new object(); //設置一個“所”
public void Add()
{
while (true)
{
lock (objLock)
{
_Num++;
Thread.Sleep(1000);
Console.WriteLine(Thread.CurrentThread.Name + ": " + _Num);
}
}
}
}
}
/*
* 多線程的“死鎖”與“同步”問題
* 死鎖:多個線程爭搶公共資源造成的異常情況
* 使用lock關鍵字讓多個線程順序訪問公共資源,暨線程的“同步”
*
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace A18_IndepthStudyMultitheading
{
class Demo1
{
public void Test1()
{
Console.WriteLine("測試與實驗多線程的死鎖問題1");
TestClass testObj = new TestClass();
Thread t1 = new Thread(testObj.ChangeState);
t1.Start();
Thread t2 = new Thread(testObj.ChangeState);
t2.Start();
}
public void Test2()
{
Console.WriteLine("測試與實驗多線程的死鎖問題2");
TestClass2 testObj = new TestClass2();
Thread t1 = new Thread(testObj.Add);
t1.Name = "TA";
t1.Start();
Thread t2 = new Thread(testObj.Add);
t2.Name = "TB";
t2.Start();
}
static void Main1(string[] args)
{
Demo1 obj = new Demo1();
//obj.Test1();
obj.Test2();
}
}
}
/*
* 線程池 和 任務
* 線城池中的線程都是後臺線程
* 線城池中的線程不能更改爲前臺線程,也不能更改優先級
*
*
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace A18_IndepthStudyMultitheading
{
class Demo2
{
public void ThreaMethod1(object para)
{
Console.WriteLine("開始下載(當前線程編號):" + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(3000);
Console.WriteLine("下載完畢!!!");
}
public void ThreadMethod2()
{
Console.WriteLine("開始下載");
Thread.Sleep(2000);
Console.WriteLine("下載完畢!!");
}
//線城池的使用
public void Test1()
{
//使用線程池
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
ThreadPool.QueueUserWorkItem(ThreaMethod1, 123);
Console.ReadLine();
}
//任務的使用
public void Test2()
{
Task t = new Task(ThreadMethod2);
t.Start();
Console.ReadLine();
}
//任務工廠的使用
public void Test3()
{
TaskFactory ft = new TaskFactory();
Task t = ft.StartNew(ThreadMethod2);
Console.ReadLine();
}
static void Main(string[] args)
{
Demo2 obj = new Demo2();
//obj.Test1();
obj.Test2();
}
}
}