线程,任务,同步之Thread(二)

时间:2022-09-10 18:32:52
/*
线程,任务,同步之Thread(二)
*/
using System;
using System.Threading;
using System.Diagnostics;

namespace Frank
{
	public class Test
    {
		//程序入口
        public static void Main(string[] args)
        {
			/*
			//Thread t1 = new Thread(ThreadMethod);
			Thread t1 = new Thread(()=>{Console.WriteLine("2");});//使用Lambda表达式
			t1.Start();

			Thread t2 = new Thread(ThreadMainWithParameters);//带参数的线程,无返回值,必须有一个是object的参数
			t2.Start(1);
			*/
			
			//把线程设置为后台线程
			Thread t3 = new Thread(ThreadMethod2);
			t3.Name = "MyNewThread";
			t3.IsBackground = true;
			t3.Start();
			Console.WriteLine("Main thread ending now.");
		}
		static void ThreadMethod()
		{
			Console.WriteLine("1");
		}
		static void ThreadMainWithParameters(object o)
		{
			Console.WriteLine(o);
		}
		static void ThreadMethod2()
		{
			Console.WriteLine("Thread {0} start",Thread.CurrentThread.Name);
			Thread.Sleep(3000);
			Console.WriteLine("Thread {0} completed",Thread.CurrentThread.Name);
		}
	}
}