/*
* 测试线程的“死锁”,及解决方法
*
*
*
* **/
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();
}
}
}