page
l 、新建控 制台工程命名为 ConsoleApplicationContro。l
2、在 Program 类中添加
public static Thread sleeper; public static Thread awaker;
3、分别添加 两个方法, 代码如下:
public void SleepThread()
{
for (inti l; i < 10; i++)
{
Console. Write(i+“,”); if(i 411 i 8)
{
Console. WriteLine("Thread is sleep at "+i); Thread.Sleep(20);
}
}
}
public void AwakeThread()
{
for(char ch ‘A’; ch <‘K’; ch++)
{
Console.Write(ch + “,”);
if (sleeper.ThreadState System.Threading. ThreadState. WaitSleepJoin)
{
Console.WriteLine(“Thread is awake at”+ ch); sleeper.Interrupt();
}
}
}
4、给 main 函数添加代码:
static void Main(string[] args)
{
Program me new Program();
sleeper new Thread(new ThreadStart(me.SleepThread)); awaker new Thread(new ThreadStart(me.AwakeThread)); sleeper. Start();
awaker. Start(); Console.ReadLine();
}
5、执行代码, 演示异常的出现。截 图。
Co ncc尸 W rite(i+‘’ ,"); if (i == 411 i == 8)
{
C汇 口尸 W riteli ne( "Thread is sleep at "+i); Thread.Sleep(20);
皿`
-
W缸 U豹h,0 1引述 X
线阻已从等帘状态宁朊
public void Awak.eThread()
{ , 甜 C 尸 c m 心 ,, o, ,;, 飞
lo「(char ch =‘A’; ch <'K 氐;,吓
{ 平 ’ 玉 飞i
C’-lI、1s nl1c. Write (ch + "
( 心吝、如 殍,飞" " 卢, o 压 ,归巨 ( 飞
ii (sleeper.ThreadState == S ystem.Thread ign
{
『、I『 e adS ta te.Wai tSlee pJoin)
Co飞 o!E荔 W riteli ne (“Thread 1s awake at”+ ch); s lee pe「,Interrupt();
6、处理由sleeper.Interput()触发的系统异常修改。SleepThread()函数, 代码如下:
public void SleepThread()
{
for (inti I; i < 10; i++)
{
Console. Write(i+“,”) if(i 411 i 8)
{
Console. WriteLine("Thread is sleep at "+i); try
{
Thread.Sleep(20);
}
catch (ThreadlnterruptedException e)
{
Console. WriteLine(“Thread is interrupted!”);
}
page
l、新建控制台工程命名为 ConsoleApplicationCreateThread。
2、给 main 泾数添加如下代码 :
static void Main(string[] args)
{
Thread.CurrentThread.Name = “主线程”;
Thread objThread = new Thread(new ThreadStart(ActionMethod)); objThread.Name = "子线程’;
objThread.Start();
ActionMethod(); Console.ReadLine();
}
3、添加命名空间 using System.Threading;
4、添加函数 ActionMethod()代码如下: static void ActionMethod()
{
for (int count = l; count <= 5; count++)
{
Console. WriteLine(“{0} 第 { l } 次”,Thread.CurrentThread.Name, count);
}
}
5、执行代码。观察执行结果。运行若干播放器, 查看执行结果。
page
l 、新建控制台工程, 命名为ConsoleApplicationReaderWriterLock。
添加命名空间using System.Threading;
2、给工程添加 Resource 类代码如下:
class Resource
{
ReaderWriterLock rwl new ReaderWriterLock(); int count O;
public void Read()
{
rw1.AcquireReaderLock(Timeout.Infinite); //请求读锁
try
{
Console.WriteLine(“+ {O} 进入读方法! count {1}”, Thread.CurrentThread.Name, count);
Thread.Sleep(SOO);
}
finally
{
rwl.ReleaseLock(); //释放读锁
Console.WriteLine("- {O} 离开读方法! ",Thread.CurrentThread.Name);
}
}
public void Write()
{
rw1.Acquire WriterLock(Timeout.Infinite); try
{
count++;
Console.WriteLine(“+ {O} 进入写方法! count {1}”, Thread.CurrentThread.Name, count); Thread.Sleep(SOO);
}
finally
{
rwl .ReleaseWriterLock();
Console.WriteLine("- {O} 离开写方法! ",Thread.CurrentThread.Name);
}
}
}
2、给工程的 Main 方法添加代码:
static void Main(string[] args)
{
Resource r new Resource();
Thread tl new Thread(new ThreadStart (r.Write)); Thread t2 new Thread(new ThreadStart(r.Write)); Thread t3 new Thread(new ThreadStart(r.Read)); Thread t4 new Thread(new ThreadStart(r.Read));
t l. Name "写线程 l "; t2.Name “写线程 2”; t3.Name "读线程 l "; t4.Name “读线程 2”; tl.Start();
t2.Start();
t3. Start();
t4.Start();
Console.ReadLine();
}
3、执行代码观察结果。
page
l 、新建控 制台工程命名为 ConsoleApplicationSynchronize。
2、给工程添加两个命名空间, 代码如下:
using System.Threading;
using System.Runtime.Remoting.Contexts; //要使用 Synchronization , 需要引用该命名空间
3、给该工程添加类 CountClass 代码如下: class CountClass
{
public int count 42;
public void ReadTask() //读取 count 的值
{
for (inti l; i < 5; i++)
{
Console. WriteLine(“{O}:Count {1}”,Thread.CurrentThread.Name,count);
}
}
public void WriteTask() //修改 count 的值
{
for (inti l; i < 5; i++)
{
count++;
Console.WriteLine(“{O}: Count {l}”, Thread.CurrentThread.Name, count);
}
}
4、给工程添加 main 方法的代码:
class Program
{
static void Main(string[] args)
{
CountClass c new CountClass();
Thread readThread new Thread(new ThreadStart(c.ReadTask)); readThread.Name “读线程”;
Thread writeThread new Thread(new ThreadStart(c.WriteTask)); writeThread.Name “写线程”;
readThread. Start();
writeThread.Start(); Console.ReadLine();
}
}
5、执行程序。观看结果。
6、修改 CountClass 的定义, 如下:( 使用 SynchronizationAttribute 类的构迼函数对驻留在上下文中、符合上下文规则的对象启用简单的自动同步)
[Synchronization]
class CountClass : ContextBoundObject
7、执行看结果。
8、将第 6 步的代码 汪释掉。在 CountClass 中添加 monitor 代码:
class CountClass
public int count 42;
public void ReadTask() //读取 count 的值
Monitor.Enter(this); //读进程先进入 Monitor 锁定的代码区同步区域, 当读线程释放了 Monitor 锁后, 写进程才开始执行
Console. WriteLine(" 进入代码同步区");
for (inti l; i < 5; i++)
{
Console. WriteLine(“{0}:Count {1}”, Thread.CurrentThread.Name, count);
}
Console. WriteLine(" 退出代码同步区");
Monitor.Exit(this);
public void WriteTask() //修改 count 的值
{
Monitor.Enter(this);
Console. WriteLine(" 进入代码同步区");
for (inti l; i < 5; i++)
{
count++;
Console.WriteLine(“{O}: Count {l}”, Thread.CurrentThread.Name, count);
}
Console. WriteLine(" 退出代码同步区");
Monitor.Exit(this);
}
9、执行代码。
10、将第 8 步中的代码汪释掉。在 CountClass 添加 lock 代码:
class CountClass
{
//: ContextBoundObject
public int count 42;
public void ReadTask() //读取 count 的值
{
lock (this)
{
for (inti l; i < 5; i++)
{
Console.WriteLine(“{O}:Count {l}”, Thread.CurrentThread.Name, count);
}
}
}
public void WriteTask() //修改 count 的值
{
lock (this)
{
for (inti I; i < 5; i++)
{
count++;
Console.WriteLine(“{O}: Count {!}”, Thread.CurrentThread.Name, count);
}
}
}
}
page
l 、新建控制台工程命名为 ConsoleApplicationThreadPool 。
2、给工程添加线程函数订线程函数
static void ThreadProc(Object statelnfo)
{
订线程函数向控制台输出
Console.WriteLine(“Hello from the thread pool.”);
}
3、修改 main 函数,代 码 如 下 :
static void Main(string[] args)
{
//将任务加入线程池的任务队列
ThreadPool.QueueUserWorkitem(new WaitCallback(ThreadProc)); Console.WriteLine(“Main thread does some work, then sleeps.”);
II 主线程等待
Thread.Sleep(SOO);
Console. WriteLine(“Main thread exits.”); Console.ReadLine();
}