Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12402964
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: WINDOWS

2012-08-20 13:54:54

 

1、一些Thread基本理论

  一个进程可以创建一个或多个线程以执行与该进程关联的部分程序代码。使用 委托或 委托指定由线程执行的程序代码。使用 委托可以将数据传递到线程过程。

  在线程存在期间,它总是处于由 定义的一个或多个状态中。可以为线程请求由 定义的调度优先级,但不能保证操作系统会接受该优先级。

提供托管线程的标识。在线程的生存期内,无论获取该值的应用程序域如何,它都不会和任何来自其他线程的值冲突。

 

2、案例代码分析

在主线程中调用其它线程,执行的过程有取决于操作系统的调度及时间的分配。

   图1,t.join()开启时,调用t的主线程main主线程会被阻塞,等待t线程执行完才去执行最后一句控制台输出;

   图2,t.join()关闭,调用t的主线程main主线程会不会阻塞,执行最后一台控制台输出完全不受T线程限制,没等t线程执行完就输出了;

   图3,t.start()后加了一个Thread.sleep(1),使main主线程休眠1秒(期间t线程照常执行)。


 


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;

  6. namespace Threaddeom
  7. {

  8.     ///
  9.     /// Simple threading scenario: Start a static method running
  10.     /// on a second thread.
  11.     ///
  12.     public class ThreadExample
  13.     {

  14.         ///
  15.         /// The ThreadProc method is called when the thread starts.
  16.         /// It loops ten times, writing to the console and yielding 
  17.         /// the rest of its time slice(时间片) each time, and then ends.
  18.         ///
  19.         public static void ThreadProc()
  20.         {
  21.             for (int i = 0; i < 10; i++)

  22.             {
  23.                 Console.WriteLine("ThreadProc: {0}", i);
  24.                 /* Yield the rest of the time slice. */
  25.                 Thread.Sleep(0);
  26.             }
  27.         }

  28.         class Program
  29.         {
  30.             static void Main(string[] args)
  31.             {
  32.                 while (true)
  33.                 {
  34.                     Console.WriteLine("Main thread: Start a second thread.");
  35.                     /*
  36.                     * The constructor for the Thread class requires a ThreadStart
  37.                     * delegate that represents the method to be executed on the
  38.                     * thread. C# simplifies the creation of this delegate,简化了委托的创建.
  39.                      */
  40.                     Thread t = new Thread(new ThreadStart(ThreadProc));
  41.                     /*
  42.                     * Start ThreadProc. Note that on a uniprocessor, the new
  43.                     * thread does not get any processor time until the main thread
  44.                     * is preempted or yields. Uncomment the Thread.Sleep that
  45.                     * follows t.Start() to see the difference.
  46.                      */
  47.                     t.Start();
  48.                     /* 通过Thread.Sleep可以让主线程休眠,从而优先完次线程的内容 */
  49.                     //Thread.Sleep(1);

  50.                     for (int i = 0; i < 4; i++)
  51.                     {
  52.                         Console.WriteLine("Main thread: Do some work.");
  53.                         Thread.Sleep(0);
  54.                     }

  55.                     Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
  56.                     /* 确保线程执行完之后,才执行下面主线程的控制台输出 */
  57.                     t.Join();
  58.                     Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
  59.                     Console.ReadLine();

  60.                     Console.Clear();
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }

 

 

image

                               图1 join开启

 

image

                            图2 join关闭

 

image

                        图3 Thread.sleep(1)开启


4、源工程代码

 Threaddemo.rar   


阅读(1848) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~