多线程Thread例子

来源:互联网 发布:php webshell 编辑:程序博客网 时间:2024/05/07 15:34
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Threading; //引入命名空间
namespace ConsoleApplication2
{
    class Program
    {
        static int s;
        static void Main(string[] args)
        {
            s =int.Parse( Console.ReadLine());
            Thread thisThread = Thread.CurrentThread;//获取当前线程
            thisThread.Name = "Main Thread";
            ThreadStart start = new ThreadStart(StartMethod); //实例化一个委托 线程委托
            Thread workerThead = new Thread(start);//创建一个线程
            workerThead.Name = "Work Thread";
            workerThead.Start();
            workerThead.Priority = ThreadPriority.Highest;//设置线程优先级
            DisplayNumbers();
            Console.ReadLine();
        }
        static void StartMethod()
        {
            DisplayNumbers();
            Console.WriteLine("Worker Thread Finished");
        }
        
        static void DisplayNumbers()
        {
            for (int i = 0; i < s; i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name+":"+i);
            }
        }
    }
    
    
}
原创粉丝点击