zoukankan      html  css  js  c++  java
  • 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


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

  • 相关阅读:
    笔记本连接蓝牙音箱声音异常
    fence安装中遇到的问题
    ssm整合关键
    第二章:数字系统
    第一章:计算器系统体系结构
    第二章:变量和基本类型
    第一章:开始
    第十九章:特殊工具与技术
    第十八章: 用于大型程序的工具
    第十七章:标准库特殊设施
  • 原文地址:https://www.cnblogs.com/emituofo/p/2246804.html
Copyright © 2011-2022 走看看