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 }
  • 相关阅读:
    [学习笔记] php设计模式总结 [转]
    [学习笔记] Windows下搭建PHP开发环境[转载]
    [学习笔记] PHP中this,self,parent的区别 [转载]
    [学习笔记] mysql连接数据库[转]
    [学习笔记] Memcache [转载]
    [学习笔记] PHPUnit 使用方法 [转载]
    [学习笔记] 正则表达式30分钟入门教程 [转]
    [学习笔记] Linux软连接和硬链接 [转]
    HLSL Matrix变量的存储方式
    Hieroglyph3 框架分析2
  • 原文地址:https://www.cnblogs.com/larry-xia/p/9213614.html
Copyright © 2011-2022 走看看