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);
                    }
                }
            }
        }
  • 相关阅读:
    springboot整合mybatis增删改查(一):项目创建
    springboot结合开源editor.md集成markdonw编辑器
    springboot发送邮件
    史上最全web.xml配置文件元素详解
    一套简约漂亮的响应式博客园主题皮肤分享给你们(二)
    一套简约漂亮的响应式博客园主题皮肤分享给你们(一)
    IDEA中项目统一编码格式设置
    windows上安装Gradle并配置环境变量
    linux自学(九)之开始centos学习,安装数据库MariaDB
    linux自学(七)之开始ccentos学习,安装jdk
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5600189.html
Copyright © 2011-2022 走看看