Class1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 1k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Threading;
  3. namespace ch9_7
  4. {
  5. /// <summary>
  6. /// Class1 的摘要说明。
  7. /// </summary>
  8. class Class1
  9. {
  10. public  static HandleData data=new HandleData();
  11. public static void ThreadMethod1()
  12. {
  13.     Console.WriteLine("第一个工作线程开始执行");
  14.         Console.WriteLine("第一个工作线程准备调用HandleData的WriteData方法");
  15. data.WriteData();
  16. Console.WriteLine("第一个工作线程返回");
  17. }
  18. public static void ThreadMethod2()
  19. {
  20. Console.WriteLine("第二个工作线程开始执行");
  21. Console.WriteLine("第二个工作线程准备调用HandleData的WriteData方法");
  22. data.WriteData();
  23. Console.WriteLine("第二个工作线程返回");
  24. }
  25. /// <summary>
  26. /// 应用程序的主入口点。
  27. /// </summary>
  28. [STAThread]
  29. static void Main(string[] args)
  30. {
  31. ThreadStart threadMethod1=new ThreadStart(ThreadMethod1);
  32. ThreadStart threadMethod2=new ThreadStart(ThreadMethod2);
  33. Console.WriteLine("现在开始创建并启动两个工作线程");
  34. Thread t1=new Thread(threadMethod1);
  35. Thread t2=new Thread(threadMethod2);
  36. t1.Start();
  37. t2.Start();
  38. }
  39. }
  40. class  HandleData
  41. {
  42. public  void  WriteData()
  43. {
  44. // Monitor.Enter(this);
  45. // Console.WriteLine(" WriteData method 被调用");
  46. // Console.WriteLine(" WriteData method 结束执行");
  47. // Monitor.Exit(this);
  48. lock(this)
  49. {
  50. Console.WriteLine(" WriteData method 被调用");
  51. Console.WriteLine(" WriteData method 结束执行");
  52. }
  53. }
  54. }
  55. }