zoukankan      html  css  js  c++  java
  • 多线程(二)启动线程:需要传参和不需要传参两种情况

    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();
            }
        }
  • 相关阅读:
    简单的2D变形 CSS transform transition
    利用文字阴影实现火焰字
    图片拖拽的继承,引用 3
    图片拖拽的继承,引用 2
    图片拖拽的继承,引用 1 (需要引入2,3两个js才能运行)
    GNU make
    GDB
    1.GCC程序编译
    设计模式之装饰者模式
    设计模式之观察者模式
  • 原文地址:https://www.cnblogs.com/25miao/p/9862525.html
Copyright © 2011-2022 走看看