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();
            }
        }
  • 相关阅读:
    jdbctemplate的batchUpdate使用方法
    js利用html5的canvas实现图像等比例压缩
    js前台通过EXIF.js获取图片中携带的经纬度信息
    Linux expr相关
    expect用于scp传输文件
    linux 自动登录ftp 获取文件
    作业8:单元测试练习(个人练习)
    作业7-用户体验设计案例分析
    作业6:团队作业——学生成绩录入系统设计与实现
    作业5:需求分析
  • 原文地址:https://www.cnblogs.com/25miao/p/9862525.html
Copyright © 2011-2022 走看看