• C#多线程(入门篇)


    昨天一同学问我相关多线程的东西,真是菜鸡互啄呀!我想想还是自己整理整理吧,也是复习下。菜鸟勿喷!

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading;
      6 using System.Runtime.Remoting.Messaging;
      7 
      8 namespace ThreadTest
      9 {
     10     public delegate void DelegateHandler(string a, string b);
     11     public class Program
     12     {
     13         static void Main(string[] args)
     14         {
     15             //创建线程原始写法,每次启用新的线程都要实例化一个Thread对象
     16             Thread threadA = new Thread(new ThreadStart(PrintA));
     17             threadA.Start();
     18 
     19             //创建一个线程,没区别,c#的语法糖写法(本人一直用的这个)
     20             Thread threadB = new Thread(PrintB);
     21             threadB.Start();
     22 
     23             //最简化写法
     24             new Thread(PrintC).Start();
     25 
     26             /*
     27              * Task在.net 4.0后代替了ThreadPool(线程池)
     28              * 程序员嫌每次启动一个线程麻烦,则先new多个线程放到线程池里,
     29              * 可以理解为放到一个List里(泛型:List<Thread>),调用函数的时候则从List中取出一个空闲的线程
     30              * 执行完毕再放回去。
     31             */
     32             System.Threading.Tasks.Task.Factory.StartNew(PrintD);
     33 
     34             //带一个参数,函数参数一定要是object对象
     35             Thread threadN = new Thread(PrintN);
     36             threadN.Start("N");
     37 
     38             //带一个参数,函数参数一定要是object对象
     39             Thread threadM = new Thread(new ParameterizedThreadStart(PrintN));
     40             threadM.Start("M");
     41 
     42             /*
     43              *带多个参数
     44              *方法1:参数放数组里传递
     45              *方法2:新建个无参方法,来调用多参数函数,参数在无参方法里传值(个人认为最笨方法)
     46              *方法3:新建个类,把被调用的函数和多个参数写在这个类里,主程序调用(与方法2如出一辙)
     47              *方法4:委托
     48              *网上还有好多其他方法,这里用委托来实现下
     49             */
     50             DelegateHandler dh = new DelegateHandler(PrintQ);
     51             dh("P", "Q");
     52 
     53             for (int i = 0; i < 1000; i++)
     54             {
     55                 Console.Write("X");
     56             }
     57             Console.ReadKey();
     58         }
     59         #region Print
     60         static void PrintA()
     61         {
     62             for (int i = 0; i < 1000; i++)
     63             {
     64                 Console.Write("A");
     65             }
     66         }
     67 
     68         static void PrintB()
     69         {
     70             for (int i = 0; i < 1000; i++)
     71             {
     72                 Console.Write("B");
     73             }
     74         }
     75 
     76         static void PrintC()
     77         {
     78             for (int i = 0; i < 1000; i++)
     79             {
     80                 Console.Write("C");
     81             }
     82         }
     83 
     84         static void PrintD()
     85         {
     86             for (int i = 0; i < 1000; i++)
     87             {
     88                 Console.Write("D");
     89             }
     90         }
     91 
     92         static void PrintN(object obj)
     93         {
     94             for (int i = 0; i < 1000; i++)
     95             {
     96                 Console.Write(obj.ToString());
     97             }
     98         }
     99 
    100         static void PrintQ(string a, string b)
    101         {
    102             for (int i = 0; i < 1000; i++)
    103             {
    104                 Console.Write(a + b);
    105             }
    106         }
    107         #endregion
    108     }
    109 }

    更多代码,更新ing...

  • 相关阅读:
    Base64编码字符串时数据量明显变大
    Javascript中的location.href有很多种用法
    javascript中top、clientTop、scrollTop、offsetTop的讲解
    jQuery事件绑定和委托
    手机页面滑动加载数据
    阻止冒泡
    页面刷新方法
    forward 和redirect的区别
    字符转换
    导出功能
  • 原文地址:https://www.cnblogs.com/gothic/p/5830381.html
Copyright © 2020-2023  润新知