多线程启动线程:需要传参和不需要传参两种情况

Posted 25miao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程启动线程:需要传参和不需要传参两种情况相关的知识,希望对你有一定的参考价值。

1、不需要传参:

 class Program7
    {
        private static void ExecuteInForeground()
        {
            DateTime start = DateTime.Now;
            var sw = Stopwatch.StartNew();
            Console.WriteLine("Thread {0}: {1}, Priority {2}",
                              Thread.CurrentThread.ManagedThreadId,
                              Thread.CurrentThread.ThreadState,
                              Thread.CurrentThread.Priority);
            do
            {
                Console.WriteLine("Thread {0}: Elapsed {1:N2} seconds",
                                  Thread.CurrentThread.ManagedThreadId,
                                  sw.ElapsedMilliseconds / 1000.0);
                Thread.Sleep(500);
            } while (sw.ElapsedMilliseconds <= 5000);
            sw.Stop();
        }
        static void Main7777(string[] args)
        {
            //如果该方法不具有任何参数,则传递ThreadStart委托给构造函数。
            //使用的是这个构造函数
            //public delegate void ThreadStart()   
            var th = new Thread(ExecuteInForeground);
            th.Start();
            Thread.Sleep(1000);
            Console.WriteLine("Main thread ({0}) exiting...",
                              Thread.CurrentThread.ManagedThreadId);
            Console.ReadLine();
        }
    }

2、需要传参:

class Program8
    {
        private static void ExecuteInForeground(Object obj)
        {
            int interval;
            try
            {
                interval = (int)obj;
            }
            catch (InvalidCastException)
            {
                interval = 5000;
            }
            DateTime start = DateTime.Now;
            var sw = Stopwatch.StartNew();
            Console.WriteLine("Thread {0}: {1}, Priority {2}",
                              Thread.CurrentThread.ManagedThreadId,
                              Thread.CurrentThread.ThreadState,
                              Thread.CurrentThread.Priority);
            do
            {
                Console.WriteLine("Thread {0}: Elapsed {1:N2} seconds",
                                  Thread.CurrentThread.ManagedThreadId,
                                  sw.ElapsedMilliseconds / 1000.0);
                Thread.Sleep(500);
            } while (sw.ElapsedMilliseconds <= interval);
            sw.Stop();
        }
        static void Main(string[] args)
        {
            //如果该方法具有参数,则传递ParameterizedThreadStart委托给构造函数。
            //使用的是这个构造函数
            //public delegate void ParameterizedThreadStart(object obj)  
            var th = new Thread(ExecuteInForeground);
            th.Start(4500);
            Thread.Sleep(1000);
            Console.WriteLine("Main thread ({0}) exiting...",
                              Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("主线程执行完毕");
            Console.ReadLine();
        }
    }

 

以上是关于多线程启动线程:需要传参和不需要传参两种情况的主要内容,如果未能解决你的问题,请参考以下文章

java 方法 传参

JS sort() 函数

请问java -jar 方法如何传参?

java 方法传参时,不是必须传的参数如何设置

c++11 thread多线程创建和传参

mybatis使用@param("xxx")注解传参和不使用的区别