正在学习c#的多线程看好多的教程,也还是一头雾水,老是不达到真正的“多线程”我想要的效果是“创建10个线程,然后用这个10个线程输出1-100”以下是我的代码:
class Program
    {
        private static int i = 0;
        static void Main(string[] args)
        {
            Console.WriteLine("线程测试 输出1-100");
            Thread t;
            for (int j = 1; j <= 100; j++)
            {
                i = j;
                t = new Thread(new ThreadStart(test));
                t.Start();
                if (t.IsAlive)
                {
                    Thread.Sleep(500);
                }
                t.Abort();
                t.Join();
            }
            Console.WriteLine("操作完成,按任意键退出");
            Console.ReadKey();
        }        private static void test()
        {
            Console.WriteLine(i);
        }
    }另外:还有个正则方面的问题,就是一个验证Email的表达式 "([A-Za-z_0-9]{1,10})+\@(A-Za-z_0-9)+(cn|com|net)+?"
这个表达式不知道为什么一运行cpu马上就占到了60%以上?

解决方案 »

  1.   


        public class ThrTest
        {
            public int writenum;
            public ThrTest(int wi)
            {
                writenum = wi;
            }
            public void WriteNum()
            {
                Console.WriteLine(writenum);
            }
        }            for (int j = 1; j <= 100; j++)
                {
                    ThrTest tt = new ThrTest(j);
                    Thread t = new Thread(new ThreadStart(tt.WriteNum));
                    t.Start();
                } 
      

  2.   

    public class ThrTest
        {
            public int writenum;
            public ThrTest(int wi)
            {
                writenum = wi;
            }
            public void WriteNum()
            {
                Console.WriteLine(writenum);
            }
        } 
    class Program
        {
           
            static void Main(string[] args)
            {
                Console.WriteLine("线程测试 输出1-100");
                
               for (int j = 1; j  <= 100; j++)
                {
                    ThrTest tt = new ThrTest(j);
                    Thread t = new Thread(new ThreadStart(tt.WriteNum));
                    t.Start();
                }  
                Console.WriteLine("操作完成,按任意键退出");
                Console.ReadKey(); 
             } 
      

  3.   

    上面的有误,下面这个是正确的using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;namespace ConsoleApplication12
    {
        public class ThrTest 

    public int writenum; 
    public ThrTest(int wi) 
    { writenum = wi; } 
    public void WriteNum() 
    { Console.WriteLine(writenum); } }
        class Program
        {
            static void Main(string[] args) 

    Console.WriteLine("线程测试 输出1-100");
     for (int j = 1; j <= 100; j++) 

    ThrTest tt = new ThrTest(j); 
    Thread t = new Thread(new ThreadStart(tt.WriteNum)); 
    t.Start(); 

    Console.WriteLine("操作完成,按任意键退出");
     Console.ReadKey(); }
        }
    }
      

  4.   

    MARK
      

  5.   

    另外正则表达式会影响CPU?
    还真没碰到过
      

  6.   

    线程的Demo这个是OK啦~不过从真正的多线程来看的话,那个没有达到要求
    比如说:如果我们有N个数要输出,而不是100个,那是不是我们要创建N个线程呢,那样的话,也就失去了线程本身的意义了
    所以,我还是希望 在:“创建10个线程,然后用这个10个线程输出1-100”这个问题上指点,谢谢^_^我也不知道,这是怎么回事,但是我把那个"[A-Za-z_0-9]"改成"[A-Z_0-9]"这样的话,就不会出现高cpu占用率的情况了,这个问题真是让我头疼!
      

  7.   

    public class ThrTest 
        { 
            public void WriteNum() 
            { 
                int j=Int32.Pare(t.Name);
                for(int i=1;i<10;i++)
                {
                    Console.WriteLine(j+i); 
                    t.Sleep(100);
                }
            } 
        }             for (int j = 0; j  < 10; j++) 
                { 
                    Thread t = new Thread(new ThreadStart(WriteNum)); 
                    t.Name=j.ToString();
                    t.Start(); 
                }