哪位能给我一个简单的多线程实例:用个变量N控制线程数,N个线程去读取一个数组的每一行,将数组全部输出就可以了。最好能带点注释,用控制台程序就可以,谢谢!

解决方案 »

  1.   


    for (int m = 0; m < N; m++)
    {
    Thread t = new Thread(new ThreadStart(GetArray()));
    t.Start();
    }
      

  2.   

        public class ThreadDemo
        {
            private Thread threadOne;
            private Thread threadTwo;
            private ArrayList stringList;
            private event EventHandler OnNumberClear;//数据删除完成引发的事件
            public static void Main()
            {
                ThreadDemo demo = new ThreadDemo(1000);
                demo.Action();
            }
            public ThreadDemo(int number)
            {
                Random random = new Random(1000000);
                stringList = new ArrayList(number);
                for (int i = 0; i < number; i++)
                {
                    stringList.Add(random.Next().ToString());
                }
                threadOne = new Thread(new ThreadStart(Run));//两个线程共同做一件事情
                threadTwo = new Thread(new ThreadStart(Run));//两个线程共同做一件事情
                threadOne.Name = "线程1";
                threadTwo.Name = "线程2";
                OnNumberClear += new EventHandler(ThreadDemo_OnNumberClear);        }
            /// <summary>
            /// 开始工作
            /// </summary>
            public void Action()
            {
                threadOne.Start();
                threadTwo.Start();
            }
            /// <summary>
            /// 共同做的工作
            /// </summary>
            private void Run()
            {
                string stringValue = null;
                while (true)
                {
                    Monitor.Enter(this);//锁定,保持同步
                    stringValue = (string)stringList[0];
                    Console.WriteLine(Thread.CurrentThread.Name + "删除了" + stringValue);
                    stringList.RemoveAt(0);//删除ArrayList中的元素
                    if (stringList.Count == 0)
                    {
                        OnNumberClear(this, new EventArgs());//引发完成事件
                    }
                    Monitor.Exit(this);//取消锁定
                    Thread.Sleep(5);
                }
            }        //执行完成之后,停止所有线程
            void ThreadDemo_OnNumberClear(object sender, EventArgs e)
            {
                Console.WriteLine("执行完了,停止了所有线程的执行。");
                threadTwo.Abort();
                threadOne.Abort();        }
        }
      

  3.   

    devilli的:
    我的GetArray()函数内容类似如下:
    if (intTxtI < 20)
    {
        Console.WriteLine("{0}", arrTxt[intTxtI]);
        intTxtI++;
    }
    但有N个线程就输出N行,数组不止N行,不知道为什么?
    HDNGO 的:好像只有2个线程,并不是N个。
      

  4.   

    HDNGO :我真的不人加,按照你那方法,好像100个线程得定义100次也?
    能不能像我发的帖子的样子弄一个简单的就好,明天继续加分,今天加不了,谢谢!!
      

  5.   

    如果想加多线程数量,加大N就可以了,注释我下午再加上去.        private static string[] Arr = new string[1000];
            private static int arrindex = 0;
            const int N = 5;
            private static ManualResetEvent ent = new ManualResetEvent(false);        static void Main()
            {
                for (int i = 0; i < 1000; i++)
                {
                    Arr[i] = i.ToString();
                }            for (int threadnum = 0; threadnum < N; threadnum++)
                {
                    Thread s = new Thread(OutputN);
                    s.Name = "thread" + threadnum.ToString();
                    s.IsBackground = true;
                    s.Start();
                    Thread.Sleep(0);
                }
                Console.Read();        }        private static void OutputN()
            {
                while (arrindex < 1000)
                {
                    lock (Arr)
                    {
                        Console.WriteLine("Thread{0} Arr{1} is: {2}", Thread.CurrentThread.Name, arrindex, Arr[arrindex]);
                        arrindex++;
                    }
                    Thread.Sleep(0);
                }
            }
      

  6.   


    ...我那个是DEMO你完全可以用数组弄个循环什么的哇
      

  7.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;namespace ConsoleApplication5
    {
        class Program
        {
            static int[,] A = new int[,] { { 1, 11, 111 }, { 2, 22, 222 }, { 3, 33, 333 } };
            static void Main(string[] args)
            {
                for (int i = 0; i <= A.GetUpperBound(0); i++)
                {
                    Thread OutputAThread = new Thread(new ThreadStart(OutputA));
                    OutputAThread.Name = i.ToString();
                    OutputAThread.IsBackground = true;
                    OutputAThread.Start();
                }
                Console.Read();
            }        static void OutputA()
            {
                lock (A)
                {
                    for (int i = 0; i < 3; i++)
                        Console.Write(A[Convert.ToInt32(Thread.CurrentThread.Name), i].ToString() + " ");
                    Console.WriteLine();
                }
            }
        }
    }
      

  8.   

    非常感谢各位给我帮助,帖子已经结了,特别是much0726 的,非常符合我的要求,HDNGO 的也写得不错,
    much0726 的我有两个地方不怎么明白,
    private static ManualResetEvent ent = new ManualResetEvent(false);
    这句应该是没用的吧,
     OutputN()函数里的 while (arrindex < 1000)好像会越界,这个其实我也觉得是对的,
    可是就是越界了,不知何故。
      

  9.   

    你可以把代码中的
    lock(Arr)换成lock(ent)效果是一样的,而且ent的效率比Arr快.特别是Arr的结构比较复杂的时候很明显,这里使用的是一个简单的数组,所以感觉不出来.
    while (arrindex < 1000)不会越界的.while (arrindex < 1000)
                {
                    lock (Arr)
                    {
                        Console.WriteLine("Thread{0} Arr{1} is: {2}", Thread.CurrentThread.Name, arrindex, Arr[arrindex]);
                        arrindex++;
                    }
                    //Thread.Sleep(0);
                }这样才会越界,看来你都差不多懂了,我就不写注释了,其实也很简单啦.
      

  10.   


                while (true)
                {
                    arrindex = 0;
                    for (int threadnum = 0; threadnum < N; threadnum++)
                    {
                        Thread s = new Thread(OutputN);
                        s.Name = "thread" + threadnum.ToString();
                        s.IsBackground = true;
                        s.Start();
                        Thread.Sleep(0);
                    }
                }我这样执行都不会出错。