zoukankan      html  css  js  c++  java
  • C# 线程参数

     class ThreadSample
        {
            private readonly int _iterations;
            public ThreadSample(int iterations)
            {
                _iterations = iterations;
            }
            public void CountNumbers()
            {
                for (int i = 1; i <= _iterations; i++)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(0.5));
                    Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                var sample = new ThreadSample(10);
    
                var threadOne = new Thread(sample.CountNumbers);
                threadOne.Name = "ThreadOne";
                threadOne.Start();
                threadOne.Join();
    
                Console.WriteLine("--------------------------");
    
                var threadTwo = new Thread(Count);
                threadTwo.Name = "ThreadTwo";
                threadTwo.Start(8);
                threadTwo.Join();
    
                Console.WriteLine("--------------------------");
    
                var threadThree = new Thread(() => CountNumbers(12));
                threadThree.Name = "ThreadThree";
                threadThree.Start();
                threadThree.Join();
                Console.WriteLine("--------------------------");
    
                int i = 10;
                var threadFour = new Thread(() => PrintNumber(i));
                i = 20;
                var threadFive = new Thread(() => PrintNumber(i));
                threadFour.Start();
                threadFive.Start();
                Console.Read();
            }
    
            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("{0} prints {1}", Thread.CurrentThread.Name, i);
                }
            }
    
            static void PrintNumber(int number)
            {
                Console.WriteLine(number);
            }

  • 相关阅读:
    BZOJ1858[Scoi2010]序列操作 题解
    BZOJ3631[JLOI2014]松鼠的新家 题解
    BZOJ1036[ZJOI2008]树的统计Count 题解
    BZOJ1798[Ahoi2009]Seq 维护序列seq 题解
    BZOJ3212 Pku3468 A Simple Problem with Integers 题解
    BZOJ1012[JSOI2008]最大数maxnumber 题解
    洛谷P1080 国王游戏
    洛谷 P2296 寻找道路
    洛谷P1970 花匠
    洛谷 P1969 积木大赛
  • 原文地址:https://www.cnblogs.com/lanyubaicl/p/11181002.html
Copyright © 2011-2022 走看看