zoukankan      html  css  js  c++  java
  • 多线程05-传参

        class Program
        {
            static void Main()
            {
                var sample = new ThreadSample(10);
                var threadOne = new Thread(sample.CountNumbers);
                threadOne.Name = "ThreadOne";
                threadOne.Start();
                threadOne.Join();
                Console.WriteLine("end threadOne");

                var threadSecond = new Thread(Count);
                threadSecond.Name = "threadSecond";
                threadSecond.Start(10);
                threadSecond.Join();
                Console.WriteLine("end threadSecond");

                var threadThree = new Thread(() => CountNumbers(10));
                threadThree.Name = "threadThree";
                threadThree.Start();
                threadThree.Join();
                Console.WriteLine("end  threadThree");
            }
            static void Count(object iterations)
            {
                CountNumbers((int)iterations);
            }
            static void CountNumbers(int iterations)
            {
                for(int i=1;i<iterations;i++)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(0.5));
                    Console.WriteLine("Thread Name is {0},Prints={1}", Thread.CurrentThread.Name, i);
                }
            }
            static void PrintNumbers(int number)
            {
                Console.WriteLine(number);
            }
            class ThreadSample
            {
                private readonly int _interations;
                public ThreadSample(int interations)
                {
                    _interations = interations;
                }
                public void CountNumbers()
                {
                    for (int i = 1; i < _interations; i++)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(0.5));
                        Console.WriteLine("Thread Name is {0},Prints={1}", Thread.CurrentThread.Name, i);
                    }
                }
            }
        }
  • 相关阅读:
    Python class:定义类
    Python return函数返回值详解
    Python None(空值)及用法
    Python函数值传递和引用传递(包括形式参数和实际参数的区别)
    Python函数的定义
    Python函数(函数定义、函数调用)用法详解
    Python reversed函数及用法
    Python zip函数及用法
    Python break用法详解
    Python嵌套循环实现冒泡排序
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5600189.html
Copyright © 2011-2022 走看看