本帖最后由 gs0038 于 2014-09-15 21:19:10 编辑

解决方案 »

  1.   

    google 生产者 消费者用一个线程作为调度线程(生产者),维护一个队列,队列中放需要做的任务。开一些工作线程(消费者)从对列中取任务并且执行。
      

  2.   

    publice class ThreadTest
    {
     private Dictionary<string, bool> _ThreadList = new Dictionary<string, bool>();
    publice List<string> TypeList=new List<string>();
    publice ThreadTest()
    {
    //初始化类型
      TypeList.Add("A");
     TypeList.Add("B");
     TypeList.Add("C");
    }
    public void test(string pType)
    {
      _ThreadList[pType] = true;//表示正在处理 /*这里是每一个线程的处理内容 ,我是让他们分开执行,比如,
    类型A在执行的时候,类型B要执行,不需要等待A执行,
    而是另开一个空间给B去执行,不然的话,B执行有时会覆盖A类型数据的内容,
    目的是让他们几个类型的线程能同时执行~互不二扰,要怎么做呢?
    希望各位帮我一忙,谢谢了*/
       
       _ThreadList[pType] = false;//表示处理完成
    }
    publice void RunTest()
    {                         
                              //TypeList.Count最大是20;
                               for (int i = 0; i < TypeList.Count; i++)
                                {
                           
                                    string _type = TypeList[i];
    if (!_ThreadList.ContainsKey(_type))
                                    {
                                      _ThreadList.Add(_type, false);
                                    }
                                   if(_ThreadList[_type]) continue;//如果线程类型在执行,则跳过。在上面少了这一行
                                    Thread _t = new Thread(new ParameterizedThreadStart(test));
                                    _t.Priority = ThreadPriority.Lowest;
                                    _t.IsBackground = true;
                                    _t.Start(_type);
                                   
                                }
    }
    }
    @caozhy 
     你看我上面的是不是类似于 生产者 消费者,_ThreadList就是一个队列的管理,但是我要怎么做才能做到 A在执行时,B也要执行,但是执行的方法我怎么用队列来分开执行这些方法,或者是有没有办法 做一个方法队列呢?
      

  3.   

    生产者消费者,简单的实现可以用BlockingCollection。
    坏处,或者说好处是,对初学者来说它隐藏了一些同步细节。
    不过可以肯定的是它的代码比较简单:class Test
    {
        static void Main()
        {
            const int MaxWorkerCount = 2;
            for (int i = 0; i < MaxWorkerCount; i++)
            {
                ThreadPool.QueueUserWorkItem(WorkerThread);
            }        Console.WriteLine("空行退出...");
            string str;
            while ((str = Console.ReadLine()) != "")
            {
                workingItems.Add(str);
            }        workingItems.CompleteAdding();
            Thread.Sleep(3000);
        }    static BlockingCollection<string> workingItems = new BlockingCollection<string>();
        static void WorkerThread(object state)
        {
            foreach (string str in workingItems.GetConsumingEnumerable())
            {
                Console.WriteLine("T{0} processing {1}...", Thread.CurrentThread.ManagedThreadId, str);
                Thread.Sleep(2000);
            }
            Console.WriteLine("T{0} ended.", Thread.CurrentThread.ManagedThreadId);
        }
    }
      

  4.   

    对“队列”没有关系。#1楼告诉你了,就是“操作不同的对象不就互不干扰了”。你给出的代码,没有一点说明了 test 方法哪里操作相同的对象了(?),所以虽然你罗列了一堆概念,但是并没有说明最简单的问题。你说不出哪一个变量有冲突,别人也就懒得告诉你如何解决。