• Thread类的简单使用(Start,Sleep,Join,传参)


    Thread 类创建并控制线程,设置其优先级并获取其状态。下面以4个例子介绍一下Thread类的常用方法:

    1. Start——启动线程。

    using System;
    using System.Threading;

    class Example
    {
    public static void Loop()
    {
    for (int j = 1; j <= 20; j++)
    {
    Console.WriteLine(Thread.CurrentThread.Name + " j = " + j);
    }
    }

    public static void Main()
    {
    Thread.CurrentThread.Name = "MainThread";
    Thread newThread = new Thread(new ThreadStart(Loop));
    newThread.Name = "NewThread";
    newThread.Start();
    for (int i = 1; i <= 20; i++)
    {
    Console.WriteLine(Thread.CurrentThread.Name + " i = " + i);
    }

    Console.ReadLine();
    }
    }

    可以看到,NewThread启动后,与MainThread相互竞争使用CPU。这就是Windows系统的抢占式CPU竞争策略。

    2. Sleep——将当前线程挂起指定的时间

    using System;
    using System.Threading;

    class Example
    {
    public static void Loop()
    {
    for (int j = 1; j <= 20; j++)
    {
    Console.WriteLine(Thread.CurrentThread.Name + " j = " + j);
    }
    }

    public static void Main()
    {
    Thread.CurrentThread.Name = "MainThread";
    Thread newThread = new Thread(new ThreadStart(Loop));
    newThread.Name = "NewThread";
    newThread.Start();
    for (int i = 1; i <= 20; i++)
    {
    if (i % 3 == 0)
    Thread.Sleep(1);
    else
    Console.WriteLine(Thread.CurrentThread.Name + " i = " + i);
    }

    Console.ReadLine();
    }
    }

    可以看到,i=3时,MainThread挂起1ms,这1ms内NewThread占用CPU,j从1跑到了11。1ms后MainThread“醒了”,重新与NewThread竞争CPU的使用,结果夺回了CPU的使用权,i从4跑到了5。i=6时,又经历了上述过程。。。
    想要更好的理解Sleep方法,请阅读http://www.cnblogs.com/ilove/archive/2008/04/07/1140419.html
     

    3. Join——暂停当前线程,直到调用Join方法的线程终止为止。

    using System;
    using System.Threading;

    class Example
    {
    public static void Loop()
    {
    for (int j = 1; j <= 10; j++)
    {
    Console.WriteLine(Thread.CurrentThread.Name + " j = " + j);
    }
    }

    public static void Main()
    {
    Thread.CurrentThread.Name = "MainThread";
    Thread newThread = new Thread(new ThreadStart(Loop));
    newThread.Name = "NewThread";
    for (int i = 1; i <= 10; i++)
    {
    if (i == 5)
    {
    newThread.Start();
    newThread.Join();
    }
    else
    {
    Console.WriteLine(Thread.CurrentThread.Name + " i = " + i);
    }
    }

    Console.ReadLine();
    }
    }

     

    可以看到,i=5时,NewThread开始并调用Join方法暂停了MainThread,直到NewThread终止,MainThread才恢复执行。

    4. 为线程传递参数

    using System;
    using System.Threading;

    class Example
    {
    public static void Say(string message)
    {
    Console.WriteLine("I'm " + Thread.CurrentThread.Name + ".");
    Console.WriteLine("I got a message: " + message);
    }

    public static void Main()
    {
    Thread.CurrentThread.Name = "MainThread";
    Thread newThread = new Thread(new ThreadStart(() => Say("Hello!")));//为线程传递参数的最简单的方法莫过于执行一个lambda表达式
    newThread.Name = "NewThread";
    newThread.Start();
    newThread.Join();

    Console.ReadLine();
    }
    }

    这里使用lambda表达式为NewThread传递参数,还有另外的方法@http://www.cnblogs.com/LoveJenny/archive/2011/05/21/2052556.html


    说明:以上部分实验不具有重复性,请读者不要纠结于:自己实验的结果与本文不同。

  • 相关阅读:
    java的instanceof简单使用
    PHP中return,exit,die的区别
    C#调用Dll文件中方法的简单应用
    C#的托管和非托管的简单理解
    对象造型(引用类型转换)
    面向对象3大特性的简单理解
    css3自定义滚动条背景透明
    console.time方法与console.timeEnd方法
    IE8 input X 去掉文本框的叉叉和密码输入框的眼睛图标
    JavaSript模块规范
  • 原文地址:https://www.cnblogs.com/emituofo/p/2246804.html
Copyright © 2020-2023  润新知