首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

惯用线程模型

2013-04-21 
常用线程模型C#using Systemusing System.Threadingpublic class Worker{// This method will be called

常用线程模型

C#

using System;using System.Threading;public class Worker{    // This method will be called when the thread is started.    public void DoWork()    {        while (!_shouldStop)        {            Console.WriteLine("worker thread: working...");        }        Console.WriteLine("worker thread: terminating gracefully.");    }    public void RequestStop()    {        _shouldStop = true;    }    // Volatile is used as hint to the compiler that this data    // member will be accessed by multiple threads.    private volatile bool _shouldStop;}public class WorkerThreadExample{    static void Main()    {        // Create the thread object. This does not start the thread.        Worker workerObject = new Worker();        Thread workerThread = new Thread(workerObject.DoWork);        // Start the worker thread.        workerThread.Start();        Console.WriteLine("main thread: Starting worker thread...");        // Loop until worker thread activates.        while (!workerThread.IsAlive);        // Put the main thread to sleep for 1 millisecond to        // allow the worker thread to do some work:        Thread.Sleep(1);        // Request that the worker thread stop itself:        workerObject.RequestStop();        // Use the Join method to block the current thread         // until the object's thread terminates.        workerThread.Join();        Console.WriteLine("main thread: Worker thread has terminated.");    }}

?

热点排行