zoukankan      html  css  js  c++  java
  • c#多线程(二)

    代码
     #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
  • 相关阅读:
    C#调用Exe文件的方法及如何判断程序调用的exe已结束(转)
    C# Color (转)
    【666】语义分割减少过拟合
    【665】构建多损失函数
    libc timer
    分支管理
    MULLS:论文阅读
    微信支付宝整合支付开发中的常见问题
    IIS8中安装和使用URL重写工具(URL Rewrite)的方法
    通过Java 技术手段,检查你自己是不是被绿了...
  • 原文地址:https://www.cnblogs.com/csharponworking/p/1630110.html
Copyright © 2011-2022 走看看