zoukankan      html  css  js  c++  java
  • C#多线程编程实战1.5检测线程状态

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    //检测线程状态
    namespace Recipe5
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("starting program");
    Thread t1 = new Thread(PrintNumbersWithStatus);
    Thread t2 = new Thread(DoNothing);
    Console.WriteLine(t1.ThreadState.ToString());
    t1.Start();
    t2.Start();
    for (int i = 1; i < 10; i++)
    {
    Console.WriteLine(t1.ThreadState.ToString());
    }
    Thread.Sleep(6);
    t1.Abort();
    Console.WriteLine("a thread has been aborted");
    Console.WriteLine(t1.ThreadState.ToString());
    Console.WriteLine(t2.ThreadState.ToString());
    Console.ReadKey();
    }
    static void DoNothing()
    {
    Thread.Sleep(2000);

    }
    static void PrintNumbersWithStatus()
    {
    Console.WriteLine("starting...");
    Console.WriteLine(Thread.CurrentThread.ThreadState.ToString());
    for (int i = 1; i < 10; i++)
    {
    Thread.Sleep(TimeSpan.FromSeconds(2));
    Console.WriteLine(i);
    }
    }
    }
    }

  • 相关阅读:
    【2019/3/23】周进度报告
    第十周总结
    程序员修炼之道-从小工到专家阅读笔记01
    第九周总结
    用户模板和用户场景
    一维数组最大子数组续
    程序员的自我修养阅读笔记03
    第八周总结
    NABCD项目分析
    第七周总结
  • 原文地址:https://www.cnblogs.com/tsh292278/p/9238205.html
Copyright © 2011-2022 走看看