多线程(2)Thread

发布时间:2017-7-1 11:23:13编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"多线程(2)Thread ",主要涉及到多线程(2)Thread 方面的内容,对于多线程(2)Thread 感兴趣的同学可以参考一下。

 1 namespace ConsoleApplication17 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             var thread = new Thread(PrintNumbers); 8             thread.Start(); 9 10             Console.WriteLine("Thread Start...");11             Console.ReadKey();12         }13 14         /// <summary>15         /// 匹配委托的方法16         /// </summary>17         public static void PrintNumbers()18         {19             Console.WriteLine("Starting......");20             for (int i = 0; i < 10; i++)21             {22                 Console.WriteLine(i);23             }24         }25     }26 }
View Code

运行结果:

 

暂停线程

假如需要暂停当前线程,可以调用Thread.Sleep方法,使当前线程处于阻塞状态,如下代码:

 1 namespace ConsoleApplication17 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             var thread = new Thread(PrintNumbersWithDelay); 8             thread.Start(); 9 10             Console.WriteLine("Thread Start...");11             Console.ReadKey();12         }13 14         /// <summary>15         /// 16         /// </summary>17         public static void PrintNumbersWithDelay()18         {19             Console.WriteLine("Starting......");20             for (int i = 0; i < 10; i++)21             {22                 Thread.Sleep(TimeSpan.FromMilliseconds(1000));23                 Console.WriteLine(string.Format("{0}  {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), i));24             }25         }26     }27 }
View Code

输出结果:

合并线程

如果需要等待某个子线程执行行,主线程才继续执行时,可以使用Thread.Join方法来实现,如下代码:

 1 namespace ConsoleApplication17 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             //Thread 8             var thread = new Thread(PrintNumbersWithDelay); 9             thread.Start();10             thread.Join();11 12             Console.WriteLine("Thread Completed!");13             Console.ReadKey();14         }15 16         /// <summary>17         /// 18         /// </summary>19         public static void PrintNumbersWithDelay()20         {21             Console.WriteLine("Starting......");22             for (int i = 0; i < 10; i++)23             {24                 Thread.Sleep(TimeSpan.FromMilliseconds(1000));//线程阻塞1s,此时线程状态为WaitSleepJoin25                 Console.WriteLine(string.Format("当前时间:{0},线程状态:{1},结果:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),Thread.CurrentThread.ThreadState, i));26             }27         }28     }29 }
View Code

 输出结果:

终止线程

 如果在子线程运行过程中强制终止它,可以调用Thread.Abort方法,这会给当前子线程触发ThreadAbortException异常,导致线程被终止!

如下代码:

 1 namespace ConsoleApplication17 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             Console.WriteLine("Starting Program..."); 8             var thread = new Thread(PrintNumbersWithDelay); 9             thread.Start();10 11             Thread.Sleep(TimeSpan.FromMilliseconds(6000));12             thread.Abort();13 14             Console.WriteLine("Thread has been abort!");15             Console.ReadKey();16         }17 18         /// <summary>19         /// 20         /// </summary>21         public static void PrintNumbersWithDelay()22         {23             Console.WriteLine("Starting......");24             for (int i = 0; i < 10; i++)25             {26                 Thread.Sleep(TimeSpan.FromMilliseconds(1000));27                 Console.WriteLine(string.Format("当前时间:{0},线程状态:{1},结果:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), Thread.CurrentThread.ThreadState, i));28             }29         }30     }31 }


上一篇:2017年1-6月流水帐
下一篇:centos中apache自用常用额外配置记录(xwamp)

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款