代码改变世界

c#多线程(二)

2009-12-22 22:30  c#在路上  阅读(1549)  评论(0编辑  收藏  举报
代码
 #region 后台线程

        
static void BackGroundThread()
        {
           
// Thread theadAnother = new Thread(delegate { Console.ReadLine(); });
            
//theadAnother.Start();
        }
        
#endregion
        
        
#region 同步锁机制的方法
        
        
static List<string> itemList=new List<string>();
        
        
static void LockerListThread()
        {
            
new Thread(AddItem).Start();
            
new Thread(AddItem).Start();
        }
        
        
static void AddItem()
        {
            
for (int i=0;i<100 ;i++ )
            {
                
lock(itemList)
                    itemList.Add(
string.Format("第{0}个",i.ToString()));
            }
            
string[] itemArray;
            
lock(itemList) itemArray=itemList.ToArray();
            
foreach(string item in itemArray)
            {
                Console.WriteLine(item);
            }
        }
        
        
#endregion
        
        
#region 线程的Interrupt 阻止方法
        
        
static void InterruptThread()
        {
            Thread interThread
=new Thread(delegate()
                                          {
                                              
try 
                                              {
                                                  Thread.Sleep(Timeout.Infinite);
                                              } 
catch (ThreadInterruptedException exp) 
                                              {
                                                  Console.WriteLine(
"线程被打断");
                                              }
                                          });
            interThread.Start();
            interThread.Interrupt();
                                          
        }
        
//判断线程是否被阻止
        
//if((worker.ThreadState & ThreadState.WaitSleepJoin)>0)
            
//worker.Interrupt();
        #endregion
        
        
#region
        
        
/// <summary>
        
/// 线程状态
        
/// </summary>
        
/// <param name="ts"></param>
        public static void SimpleThreadState(ThreadState ts)
        {
//            return ts &(ThreadState.Aborted | ThreadState.AbortRequested|
//                       ThreadState.Stopped|ThreadState.Unstarted|
//                      ThreadState.WaitSleepJoin);
        }
        
        
        
#endregion
        
        
#region 同步中信号量的使用
        
        
static EventWaitHandle wh=new AutoResetEvent(false);
        
static void WaitHanderTestThread()
        {
            Thread waiterThread
=new Thread(Waiter);
            waiterThread.Start();
            Thread.Sleep(
1000);
            
//唤醒线程;
            wh.Set();
            wh.Close();
        }
        
        
static void Waiter()
        {
            Console.WriteLine(
"Sleeping ......");
            
//等待通知
            wh.WaitOne();
            Console.WriteLine(
"Notifying .....");
        }
        
#endregion
        
        
#region 信号使用于任务分解
        
        
static EventWaitHandle ready=new AutoResetEvent(false);
        
static EventWaitHandle go=new AutoResetEvent(false);
        
static volatile string task="";
        
static void WaitEventHandlerThreadTest()
        {
            
//一个任务分解为多个线程来完成
            new Thread(DoWorker).Start();
            
for (int i=0;i<5 ;i++ ) 
            {
                ready.WaitOne();
//首先工作线程等待,直到工作线程准备好。
                task="a".PadRight(i,'h'); //给工作任务赋值
                go.Set();  //然后开始执行
            }
            ready.WaitOne();task
=null;go.Set();
        }
        
        
static void DoWorker()
        {
            
while(true)
            {
                ready.Set(); 
//说明该线程已经准备好。
                go.WaitOne(); //然后等待退出
                if(task==null)return//退出线程
                Console.WriteLine(task);
            }
        }
        
#endregion
        
        
#region 测试线程jion方法
        
        
static void JoinThreadTest()
        {
            Thread t
=new Thread(delegate()
                       {
                                    
//while(true)
                                    
//{
                                        Console.ReadLine();
                                    
//}
                       });
            t.Start();
            t.Join();
            Console.WriteLine(
"Thread ReadLine is complete!");
        }
        
        
#endregion