下一页 1 2
线程处理的优点是可以创建使用多个执行线程的应用程序。例如,某一进程可以具有管理与用户交互的用户界面线程,以及在用户界面线程等待用户输入时执行其他任务的辅助线程。 该教程说明各种线程活动:
该教程包含下列示例:
本示例说明如何创建和启动线程,并显示了同时在同一进程内运行的两个线程间的交互。请注意,不必停止或释放线程。这由 .NET Framework 公共语言运行库自动完成。 程序从创建 下面的示例显示如何使用 C# lock 关键字和 Monitor 对象的 Pulse 方法完成同步。Pulse 方法通知等待队列中的线程对象的状态已更改。(有关脉冲的更多详细信息,请参见 Monitor.Pulse 方法)。 本示例创建一个
教程
示例 1:创建线程、启动线程和线程间交互
Alpha
类型的对象 (oAlpha
) 和引用 Alpha
类的 Beta
方法的线程 (oThread
) 开始。然后启动该线程。线程的 IsAlive
属性允许程序等待,直到线程被初始化(被创建、被分配等)为止。主线程通过 Thread
访问,而 Sleep
方法通知线程放弃其时间片并在一定毫秒数期间停止执行。然后 oThread
被停止和联接。联接一个线程将使主线程等待它死亡或等待它在指定的时间后过期。最后,程序尝试重新启动 oThread
,但由于线程无法在停止(中止)后重新启动而告失败。有关临时停止执行的信息,请参见挂起线程执行。// StopJoin.cs
using System;
using System.Threading;
public class Alpha
{
// This method that will be called when the thread is started
public void Beta()
{
while (true)
{
Console.WriteLine("Alpha.Beta is running in its own thread.");
}
}
};
public class Simple
{
public static int Main()
{
Console.WriteLine("Thread Start/Stop/Join Sample");
Alpha oAlpha = new Alpha();
// Create the thread object, passing in the Alpha.Beta method
// via a ThreadStart delegate. This does not start the thread.
Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
// Start the thread
oThread.Start();
// Spin for a while waiting for the started thread to become
// alive:
while (!oThread.IsAlive);
// Put the Main thread to sleep for 1 millisecond to allow oThread
// to do some work:
Thread.Sleep(1);
// Request that oThread be stopped
oThread.Abort();
// Wait until oThread finishes. Join also has overloads
// that take a millisecond interval or a TimeSpan object.
oThread.Join();
Console.WriteLine();
Console.WriteLine("Alpha.Beta has finished");
try
{
Console.WriteLine("Try to restart the Alpha.Beta thread");
oThread.Start();
}
catch (ThreadStateException)
{
Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
Console.WriteLine("Expected since aborted threads cannot be restarted.");
}
return 0;
}
}
输出示例
Thread Start/Stop/Join Sample
Alpha.Beta is running in its own thread.
Alpha.Beta is running in its own thread.
Alpha.Beta is running in its own thread.
...
...
Alpha.Beta has finished
Try to restart the Alpha.Beta thread
ThreadStateException trying to restart Alpha.Beta. Expected since aborted threads cannot be restarted.
示例 2:同步两个线程:制造者和使用者
Cell
对象,它具有两个方法:ReadFromCell
和 WriteToCell
。从 CellProd
和 CellCons
类创建另外两个对象;这两个对象均具有调用 ReadFromCell
和 WriteToCell
的 ThreadRun
方法。通过等待依次到达的来自 Monitor 对象的“脉冲”即可完成同步。也就是说,首先产生一个项(此时使用者等待脉冲),然后发生一个脉冲,接着使用者使用所产生的项(此时制造者等待脉冲),依此类推。// MonitorSample.cs
// This example shows use of the following methods of the C# lock keyword
// and the Monitor class
// in threads:
// Monitor.Pulse(Object)
// Monitor.Wait(Object)
using System;
using System.Threading;
public class MonitorSample
{
public static void Main(String[] args)
{
int result = 0; // Result initialized to say there is no error
Cell cell = new Cell( );
CellProd prod = new CellProd(cell, 20); // Use cell for storage,
// produce 20 items
CellCons cons = new CellCons(cell, 20); // Use cell for storage,
// consume 20 items
Thread producer = new Thread(new ThreadStart(prod.ThreadRun));
Thread consumer = new Thread(new ThreadStart(cons.ThreadRun));
// Threads producer and consumer have been created,
// but not started at this point.
try
{
producer.Start( );
consumer.Start( );
producer.Join( ); // Join both threads with no timeout
// Run both until done.
consumer.Join( );
// threads producer and consumer have finished at this point.
}
catch (ThreadStateException e)
{
Console.WriteLine(e); // Display text of exception
result = 1; // Result says there was an error
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e); // This exception means that the thread
// was interrupted during a Wait
result = 1; // Result says there was an error
}
// Even though Main returns void, this provides a return code to
// the parent process.
Environment.ExitCode = result;
}
}
public class CellProd
{
Cell cell; // Field to hold cell object to be used
int quantity = 1; // Field for how many items to produce in cell
public CellProd(Cell box, int request)
{
cell = box; // Pass in what cell object to be used
quantity = request; // Pass in how many items to produce in cell
}
public void ThreadRun( )
{
for(int looper=1; looper<=quantity; looper++)
cell.WriteToCell(looper); // "producing"
}
}
public class CellCons
{
Cell cell; // Field to hold cell object to be used
int quantity = 1; // Field for how many items to consume from cell
public CellCons(Cell box, int request)
{
cell = box; // Pass in what cell object to be used
quantity = request; // Pass in how many items to consume from cell
}
public void ThreadRun( )
{
int valReturned;
for(int looper=1; looper<=quantity; looper++)
// Consume the result by placing it in valReturned.
valReturned=cell.ReadFromCell( );
}
}
public class Cell
{
int cellContents; // Cell contents
bool readerFlag = false; // State flag
public int ReadFromCell( )
{
lock(this) // Enter synchronization block
{
if (!readerFlag)
{ // Wait until Cell.WriteToCell is done producing
try
{
// Waits for the Monitor.Pulse in WriteToCell
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
Console.WriteLine("Consume: {0}",cellContents);
readerFlag = false; // Reset the state flag to say consuming
// is done.
Monitor.Pulse(this); // Pulse tells Cell.WriteToCell that
// Cell.ReadFromCell is done.
} // Exit synchronization block
return cellContents;
}
public void WriteToCell(int n)
{
lock(this) // Enter synchronization block
{
if (readerFlag)
{ // Wait until Cell.ReadFromCell is done consuming.
try
{
Monitor.Wait(this); // Wait for the Monitor.Pulse in
// ReadFromCell
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
cellContents = n;
Console.WriteLine("Produce: {0}",cellContents);
readerFlag = true; // Reset the state flag to say producing
// is done
Monitor.Pulse(this); // Pulse tells Cell.ReadFromCell that
// Cell.WriteToCell is done.
} // Exit synchronization block
}
}
输出示例
Produce: 1
Consume: 1
Produce: 2
Consume: 2
Produce: 3
Consume: 3
...
...
Produce: 20
Consume: 20