- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
Class1.cs
资源名称:VC#.NET范例.rar [点击查看]
上传用户:chinafred
上传日期:2007-08-14
资源大小:10127k
文件大小:1k
源码类别:
数据库编程
开发平台:
C#
- using System;
- using System.Threading;
- namespace ch9_7
- {
- /// <summary>
- /// Class1 的摘要说明。
- /// </summary>
- class Class1
- {
- public static HandleData data=new HandleData();
- public static void ThreadMethod1()
- {
- Console.WriteLine("第一个工作线程开始执行");
- Console.WriteLine("第一个工作线程准备调用HandleData的WriteData方法");
- data.WriteData();
- Console.WriteLine("第一个工作线程返回");
- }
- public static void ThreadMethod2()
- {
- Console.WriteLine("第二个工作线程开始执行");
- Console.WriteLine("第二个工作线程准备调用HandleData的WriteData方法");
- data.WriteData();
- Console.WriteLine("第二个工作线程返回");
- }
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main(string[] args)
- {
- ThreadStart threadMethod1=new ThreadStart(ThreadMethod1);
- ThreadStart threadMethod2=new ThreadStart(ThreadMethod2);
- Console.WriteLine("现在开始创建并启动两个工作线程");
- Thread t1=new Thread(threadMethod1);
- Thread t2=new Thread(threadMethod2);
- t1.Start();
- t2.Start();
- }
- }
- class HandleData
- {
- public void WriteData()
- {
- // Monitor.Enter(this);
- // Console.WriteLine(" WriteData method 被调用");
- // Console.WriteLine(" WriteData method 结束执行");
- // Monitor.Exit(this);
- lock(this)
- {
- Console.WriteLine(" WriteData method 被调用");
- Console.WriteLine(" WriteData method 结束执行");
- }
- }
- }
- }