06.C#线程Thread

来源:互联网 发布:java string int 转换 编辑:程序博客网 时间:2024/09/21 09:18

Program.cs文件:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Collections;using System.IO;using System.Threading;namespace Test1{    class Program    {        static void Main(string[] args)        {            /*                 1.使用using System.Threading;                 2.自己摸索,可以查看api文档。             */            //1.相当于不带参数的委托delegate            Thread t1 = new Thread(new ThreadStart(Test1));            t1.Name = "线程1";                       t1.Start();            //2.相当于带参数的委托delegate            Thread t2 = new Thread(new ParameterizedThreadStart(Test2));            t2.Name = "线程2";            t2.Priority = ThreadPriority.Highest;//设置线程的优先级(前提是在线程池中)            t2.IsBackground = true;//是否在后台执行            t2.Start("lai哈喽");        }        static void Test1() {            Thread.Sleep(2000);//线程睡眠2秒            Console.WriteLine("当前线程的Name:"+Thread.CurrentThread.Name);        }        static void Test2(object s1)        {            Thread.CurrentThread.Join(1000);//线程阻塞1秒            Console.WriteLine(s1+"当前线程的Name:" + Thread.CurrentThread.Name);        }    }}
1 0
原创粉丝点击