• 向线程传递参数


    参考<<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 }
  • 相关阅读:
    单元测试
    Go 语言基础语法
    Go 切片
    Beego环境搭建和bee工具安装使用(绝对可成功)
    Go 语言安装以及BeeGo环境配置
    解决安装了Phpstudy和本地数据库冲突的问题
    Go语言入门学习指南
    day74:drf:drf其他功能:认证/权限/限流/过滤/排序/分页/异常处理&自动生成接口文档
    day73:drf:drf视图相关类&路由Routers&创建虚拟环境
    day72:drf:反序列化功能&模型类序列化器Modelserializer&drf视图APIView
  • 原文地址:https://www.cnblogs.com/larry-xia/p/9213614.html
Copyright © 2020-2023  润新知