zoukankan      html  css  js  c++  java
  • 向线程传递参数

    参考<<C#多线程编程实战>>

     1 using System;
     2 using System.Threading;
     3 
     4 namespace Chapter1.Recipe8
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             var sample = new ThreadSample(10);
    11 
    12             var threadOne = new Thread(sample.CountNumbers);
    13             threadOne.Name = "ThreadOne";
    14             threadOne.Start();
    15             threadOne.Join();
    16 
    17             Console.WriteLine("--------------------------");
    18 
    19             var threadTwo = new Thread(Count);
    20             threadTwo.Name = "ThreadTwo";
    21             threadTwo.Start(8);  //传递object类型的单个参数
    22             threadTwo.Join();
    23 
    24             Console.WriteLine("--------------------------");
    25 
    26             var threadThree = new Thread(() => CountNumbers(12));
    27             threadThree.Name = "ThreadThree";
    28             threadThree.Start();
    29             threadThree.Join();
    30             Console.WriteLine("--------------------------");
    31 
    32             //在多个lambda表达式中使用相同变量,会共享变量的值
    33             int i = 10;
    34             var threadFour = new Thread(() => PrintNumber(i));  //20
    35             i = 20;
    36             var threadFive = new Thread(() => PrintNumber(i)); //20
    37             threadFour.Start();
    38             threadFive.Start();
    39             
    40         }
    41 
    42         static void Count(object iterations)
    43         {
    44             CountNumbers((int)iterations);
    45         }
    46 
    47         static void CountNumbers(int iterations)
    48         {
    49             for (int i = 1; i <= iterations; i++)
    50             {
    51                 Thread.Sleep(TimeSpan.FromSeconds(0.5));
    52                 Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
    53             }
    54         }
    55 
    56         static void PrintNumber(int number)
    57         {
    58             Console.WriteLine(number);
    59         }
    60 
    61         class ThreadSample
    62         {
    63             private readonly int _iterations;
    64 
    65             public ThreadSample(int iterations)
    66             {
    67                 _iterations = iterations;
    68             }
    69             public void CountNumbers()
    70             {
    71                 for (int i = 1; i <= _iterations; i++)
    72                 {
    73                     Thread.Sleep(TimeSpan.FromSeconds(0.5));
    74                     Console.WriteLine("{0} prints {1}", Thread.CurrentThread.Name, i);
    75                 }
    76             }
    77         }
    78     }
    79 
    80 
    81 }
  • 相关阅读:
    数论——欧拉函数
    数论——最大公约数
    Python——循环
    数论——素数和反素数
    数论——快速幂剖析
    Perfect Keyboard
    HTML学习笔记Day6
    HTML学习笔记Day5
    HTML学习笔记Day4
    HTML学习笔记Day3
  • 原文地址:https://www.cnblogs.com/larry-xia/p/9213614.html
Copyright © 2011-2022 走看看