• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一个具有上进心的码农
因为一篇文章中有很多是从很多篇文章中摘取的,请恕我没有一一说明摘取出处,如果没有说明,则该文章默认是摘取,如有侵犯您的权益,请与我联系,将会马上删除。
博客园    首页    新随笔    联系   管理    订阅  订阅

多线程同步

在Main方法里在. 我一次性要开10个线程, 十个线程跑同一个方法, 但方法传入的参数不同, 等这十个线程都跑完了, 接下来再做Main方法里其他的事情

转载的:

我有个想法
设定一个变量t,当新建一个线程时t+1,线程完成时t-1,
线程完成时调用一个委托,
在委托方法中通过判断t是否等于0来确定所有线程是否执行完毕

在main中通过do while t <> 0来判断也可以……

 

 

这是第二种方法. 但没有测试过.

 

 

代码
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Threading; 
using System.Collections.ObjectModel; 

namespace ThreadControler 
{ 
    
class Program 
    { 
        
static void Main(string[] args) 
        { 
            m_ResultInfoList
=new Collection <string>(); 
            Collection 
<Thread> threadList=new Collection <Thread>(); 
            
for (int i = 0; i < 10; i++) 
            { 
                Thread workThread 
= new Thread(new ParameterizedThreadStart(TestThreadProc)); 
                threadList.Add(workThread); 
                workThread.Start(i); 
            } 
            m_ThreadReadyEvent.Set(); 
            
foreach (Thread tempThread in threadList) 
            { 
                tempThread.Join(); 
            } 
            
foreach (String tempResult in m_ResultInfoList) 
            { 
                Console.WriteLine(tempResult); 
            } 
            Thread.Sleep(
5000); 
        } 
        
private static Mutex m_ResultListMutex = new Mutex(); 
        
private static Collection <String> m_ResultInfoList=null; 
        
private static EventWaitHandle m_ThreadReadyEvent = new EventWaitHandle(false, EventResetMode.ManualReset); 
        
private static void TestThreadProc(Object index) 
        { 
            m_ThreadReadyEvent.WaitOne(); 
            
for (int i = 0; i < 5; i++) 
            { 
                Thread.Sleep(
500); 
            } 
            m_ResultListMutex.WaitOne(); 
            m_ResultInfoList.Add(index.ToString()); 
            m_ResultListMutex.ReleaseMutex(); 
        } 
    } 
} 

 

 

下面这个测试过. 可以用.

 

 

代码
   //设置一个信号量用于同步        static System.Threading.Semaphore sema =             new System.Threading.Semaphore(0, 10);         static void ThreadProc(object state)        {            Console.WriteLine(state);            sema.Release();        }        static void Main(string[] args)        {            for (int i = 0; i < 10; i++)            {                //Run thread                System.Threading.Thread thread =                     new System.Threading.Thread(ThreadProc);                     thread.Start(i);            }            //Waitting            for (int i = 0; i < 10; i++)            {                sema.WaitOne();            }            Console.WriteLine("All threads done");        }


 

posted @ 2009-11-26 22:43  不若相忘于江湖  阅读(200)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3