线程

创建线程

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbers);

t.Start();

 

PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


for (int i=0;i<10;i++)

{


Console.WriteLine(i);

}

}

}

}

暂停线程

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbersWithDelay);

t.Start();

 

PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


for (int i=0;i<10;i++)

{


Console.WriteLine(i);

}

}

 


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


for (int i = 0; i < 10; i++)

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i);

}

}

}

}

线程等待

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbersWithDelay);

t.Start();

t.Join();

 

PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


for (int i=0;i<10;i++)

{


Console.WriteLine(i);

}

}

 


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


for (int i = 0; i < 10; i++)

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);

}

}

}

}

终止线程

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbersWithDelay);

t.Start();


Thread.Sleep(TimeSpan.FromSeconds(6));

t.Abort();

 


//PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


for (int i=0;i<10;i++)

{


Console.WriteLine(i);

}

}

 


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


for (int i = 0; i < 10; i++)

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);

}

}

}

}

检测线程状态

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t = new
Thread(PrintNumbersWithDelay);

t.Start();


Thread.Sleep(TimeSpan.FromSeconds(6));


Console.WriteLine(t.ThreadState.ToString());

t.Abort();

 


//PrintNumbers();

}

 


static
void PrintNumbers()

{


Console.WriteLine(“Starting…”);


for (int i=0;i<10;i++)

{


Console.WriteLine(i);

}

}

 


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


for (int i = 0; i < 10; i++)

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);


Console.WriteLine(Thread.CurrentThread.ThreadState);

}

}

}

}

线程优先级

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t1 = new
Thread(PrintNumbers1);


Thread t2 = new
Thread(PrintNumbers2);

t1.Priority = ThreadPriority.Highest;

t2.Priority = ThreadPriority.Lowest;

t2.Start();

t1.Start();

 

 


//PrintNumbers();

}

 


static
void PrintNumbers1()

{


Console.WriteLine(“Starting…1”);


for (int i=0;i<10;i++)

{


Console.WriteLine(i.ToString()+“+1”);

}

}


static
void PrintNumbers2()

{


Console.WriteLine(“Starting…2”);


for (int i = 0; i < 10; i++)

{


Console.WriteLine(i.ToString() + “+2”);

}

}


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


for (int i = 0; i < 10; i++)

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);


Console.WriteLine(Thread.CurrentThread.ThreadState);

}

}

}

}

前后台程序

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Threading;

 

namespace ConsoleApplication12

{


class
Program

{


static
void Main(string[] args)

{


Thread t1 = new
Thread(PrintNumbers1);


Thread t2 = new
Thread(PrintNumbers2);

t2.IsBackground = true;

t2.Start();


Thread.Sleep(TimeSpan.FromSeconds(10));

t1.Start();

 

 


//PrintNumbers();

}

 


static
void PrintNumbers1()

{


Console.WriteLine(“Starting…1”);


for (int i=0;i<10;i++)

{


Console.WriteLine(i.ToString()+“+1”);

}

}


static
void PrintNumbers2()

{


Console.WriteLine(“Starting…2”);


for (int i = 0; i < 1000; i++)

{


Console.WriteLine(i.ToString() + “+2”);

}

}


static
void PrintNumbersWithDelay()

{


Console.WriteLine(“Starting…”);


for (int i = 0; i < 10; i++)

{


Thread.Sleep(TimeSpan.FromSeconds(2));


Console.WriteLine(i.ToString()+“###”);


Console.WriteLine(Thread.CurrentThread.ThreadState);

}

}

}

}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注