.net 多线程 类传递参数

using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime startTime = DateTime.Now;
            My m = new My();
            m.x = 2;
            m.y = 5;

            for (int i = 0; i < 30; i++)
            {
                Random rnd = new Random();
                m =new My();
                m.id = i;
                m.x = rnd.Next(0,10);
                m.y = rnd.Next(10, 40);
                Thread thread = new Thread(new ThreadStart(m.Work));
                thread.Start();
            }
            DateTime endTime = DateTime.Now;
            TimeSpan ts = endTime - startTime;
            Console.WriteLine("线程时间:" + ts.TotalSeconds);
            Console.ReadKey();
        }

        class My
        {
            public int id,x, y;
            public void Work()
            {
                Thread.Sleep(1000);
                Console.WriteLine("id={0},x={1},y={2}", this.id, this.x, this.y);
            }
        }
    }
}

  

上一篇:k8s部署tomcat


下一篇:否定伪类