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

     Thread类的使用:

    初始化:

    Thread th1 = new Thread(function1);
    Thread th2 = new Thread(new ThreadStart(function1));
    Thread th3 = new Thread(new ParameterizedThreadStart(function2));

    ThreadStart和ParameterizedThreadStart是两个委托,方法的抽象。

    function1...是方法名,在新线程里要执行的方法名。

     public void function1()
    {
    }
    public void function2(object o)
    {
         var ii = (Parameter)o;
    }

    Parameter是实体类

    public class Parameter
            {
                public string paraName { get; set; }
            }

    那么,如何启动线程呢?

    th1.Start();
    th2.Start();
    th3.Start(new Parameter { paraName="张三" });

    请注意th3的传参方式:通过Start()方法,传递object对象。

    Thread的Sleep()方法

    作用:告诉操作系统或者其他XXOO的东西,在一段时间内,本线程是睡眠状态,不参与资源的竞争。

    Thread的Join()方法

    作用:阻塞调用线程,让子线程执行,直到执行完毕,控制权交还给主线程(调用线程)继续执行。

      

  • 相关阅读:
    PHP安装linux
    nginx 安装
    Redis安装
    linux启动http服务
    收藏的有用的网页
    laravel框架部署后有用命令
    .net 报错access to the path c: empimagefilesmsc_cntr_0.txt is denied
    oracle 触发器
    学习Auxre记录
    mysql数据库索引
  • 原文地址:https://www.cnblogs.com/xyang/p/2757572.html
Copyright © 2011-2022 走看看