看看这个例子using System;
using System.Threading;namespace Thread4
{
public class Cell
{
int cellContents;
bool readerFlag=false; public void WriteToCell(int n)
{
lock(this)
{
if(readerFlag)
{
try
{
Monitor.Wait(this);
}
catch(SynchronizationLockException e)
{
Console.WriteLine(e);
}
}
cellContents=n;
Console.WriteLine("生产者{0}",cellContents);
readerFlag=true;
Monitor.Pulse(this);
}
} public int ReadFromCell()
{
lock(this)
{
if(!readerFlag)
{
try
{
Monitor.Wait(this);
}
catch(SynchronizationLockException e)
{
Console.WriteLine(e);
}
}
Console .WriteLine("消费者{0}",cellContents);
readerFlag=false;
Monitor.Pulse(this);
}
return cellContents;
}
}
public class CellProd
{
Cell cell; // 被操作的Cell对象
int quantity = 1; // 生产者生产次数,初始化为1
public CellProd(Cell box, int request)
{
//构造函数
cell = box;
quantity = request;
}
public void ThreadRun( )
{
for(int looper=1; looper<=quantity; looper++)
cell.WriteToCell(looper);
}
} public class CellCons
{
Cell cell;
int quantity=1; public CellCons(Cell box,int request)
{
cell=box;
quantity=request;
} public void ThreadRun()
{
int valReturnen;
for(int looper=1;looper<=quantity;looper++)
{
valReturnen=cell.ReadFromCell();
}
}
}
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
int result=0;
Cell cell=new Cell();
CellProd prod = new CellProd(cell, 20);
CellCons cons = new CellCons(cell, 20); Thread produer = new Thread(new ThreadStart(prod.ThreadRun));
Thread consumer = new Thread(new ThreadStart(cons.ThreadRun)); try
{
produer.Start();
consumer.Start(); produer.Join();
consumer.Join();
Console.ReadLine();
}
catch (ThreadStateException e)
{
Console.WriteLine(e);
result = 1;
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
result = 1;
}
Environment.ExitCode=result; }
}
}
using System.Threading;namespace Thread4
{
public class Cell
{
int cellContents;
bool readerFlag=false; public void WriteToCell(int n)
{
lock(this)
{
if(readerFlag)
{
try
{
Monitor.Wait(this);
}
catch(SynchronizationLockException e)
{
Console.WriteLine(e);
}
}
cellContents=n;
Console.WriteLine("生产者{0}",cellContents);
readerFlag=true;
Monitor.Pulse(this);
}
} public int ReadFromCell()
{
lock(this)
{
if(!readerFlag)
{
try
{
Monitor.Wait(this);
}
catch(SynchronizationLockException e)
{
Console.WriteLine(e);
}
}
Console .WriteLine("消费者{0}",cellContents);
readerFlag=false;
Monitor.Pulse(this);
}
return cellContents;
}
}
public class CellProd
{
Cell cell; // 被操作的Cell对象
int quantity = 1; // 生产者生产次数,初始化为1
public CellProd(Cell box, int request)
{
//构造函数
cell = box;
quantity = request;
}
public void ThreadRun( )
{
for(int looper=1; looper<=quantity; looper++)
cell.WriteToCell(looper);
}
} public class CellCons
{
Cell cell;
int quantity=1; public CellCons(Cell box,int request)
{
cell=box;
quantity=request;
} public void ThreadRun()
{
int valReturnen;
for(int looper=1;looper<=quantity;looper++)
{
valReturnen=cell.ReadFromCell();
}
}
}
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
int result=0;
Cell cell=new Cell();
CellProd prod = new CellProd(cell, 20);
CellCons cons = new CellCons(cell, 20); Thread produer = new Thread(new ThreadStart(prod.ThreadRun));
Thread consumer = new Thread(new ThreadStart(cons.ThreadRun)); try
{
produer.Start();
consumer.Start(); produer.Join();
consumer.Join();
Console.ReadLine();
}
catch (ThreadStateException e)
{
Console.WriteLine(e);
result = 1;
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
result = 1;
}
Environment.ExitCode=result; }
}
}
monitor类
mutex类
都可以实现同步
namespace 多线程1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class2 my1=new Class2();
Thread t1=new Thread(new ThreadStart(my1.printoddnumber));
Thread t2=new Thread(new ThreadStart(my1.printeventnumber));
t1.Start();
t2.Start();
}
}
}
namespace 多线程1
{
/// <summary>
/// Class2 的摘要说明。
/// </summary>
public class Class2
{
//public object o=1000;
public Class2()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void printoddnumber()
{
// Monitor.Enter(o);
Monitor.Enter(this);
for(int i=1;i<10;i+=2)
{
Monitor.Wait(this);
Console.WriteLine(i);
Monitor.Pulse(this);
}
Monitor.Exit(this);
//Monitor.Exit(o);
} public void printeventnumber()
{
// Monitor.Enter(o);
Monitor.Enter(this);
for(int i=0;i<=10;i+=2)
{
Console.WriteLine(i);
Monitor.Pulse(this);
Monitor.Wait(this);
}
Monitor.Exit(this);//Monitor.Exit(o); } }
}
public void Func()
{}