初步学习多线程操作,代码不是完美的,欢迎大牛指点(运行通过)

 如需转载,注明:Copyright ©Mr.Smart  zdwloveschina@gmail.com

//第一个program类

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Collections;
 5 using System.IO;
 6 using System.Text;
 7 using System.Threading;
 8 
 9 namespace AboutTheardTest
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             Thread t_W, t_R;
16             WriteToTxt wt = new WriteToTxt();
17             ReadFromTxt rf = new ReadFromTxt(3000);
18             t_W = new Thread(new ThreadStart(wt.first));
19             t_R = new Thread(new ThreadStart(rf.Reader));
20             t_W.Priority = ThreadPriority.Highest;
21             t_R.Priority = ThreadPriority.Normal;
22             t_W.Start();
23             t_R.Start();
24         }
25     }
26 }
复制代码

 

1
//第二个WriteToTxt类
复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 using System.Threading;
 7 
 8 
 9 namespace AboutTheardTest
10 {
11     class WriteToTxt
12     {
13         public void first()
14         {
15             int i = 1;
16             write(i);
17         }
18         public void write(int i)
19         {
20             int j = i;
21             FileStream fs = new FileStream(@"E:\ThreadTest.txt", FileMode.Append);
22             StreamWriter sw = new StreamWriter(fs);           
23             try
24             {
25                 if (i >= 51)
26                 {
27                     for (int k = i; k <= 100; k++)
28                     {
29                         Console.WriteLine("第  {0} 次写入",k);
30                        sw.WriteLine("第 {0} 次写入,I want to be a great Coder! fighter!", k);
31                        if(k == 100)
32                         {
33                             Console.WriteLine("此时是第 {0} 次写入。运行结束",k);                           
34                         }
35                     }                   
36                 }
37                 else
38                 {
39                     lock (this)
40                     {
41                         for(j = 1;j<50;j++)
42                         {
43                             Console.WriteLine("第 {0} 次写入",j);
44                             sw.WriteLine("第 {0} 次写入,I want to be a great Coder! fighter!", j);
45                         }
46                          sw.WriteLine("第 {0} 次写入,I want to be a great Coder! fighter!", j);
47                          sw.Close();
48                          fs.Close();
49                          Console.WriteLine("---------------------正在等读出操作完成---------------------------");
50                          Monitor.Wait(this);
51                         // Monitor.Pulse(this);
52                     }
53                 }            
54             }
55             catch (Exception e)
56             {
57                 Console.WriteLine("write_ERROR:" + e.Message);
58             }
59             finally
60             {
61                 sw.Close();
62                 fs.Close();
63             }
64         }
65     }
66 }
复制代码

//第三个ReadFromTxt类

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 using System.Threading;
 7 
 8 namespace AboutTheardTest
 9 {
10     class ReadFromTxt
11     {
12         int times;
13         public ReadFromTxt(int times)
14         {
15             this.times = times;
16         }
17         public void Reader()
18         {
19             Console.WriteLine("--------正在等写入完成--------");
20             Thread.Sleep(times);
21             Console.WriteLine("--------启动读出线程----------");
22             string strLine;
23             FileStream Fs = new FileStream(@"E:\ThreadTest.txt", FileMode.Open);
24             StreamReader sr = new StreamReader(Fs);
25             try
26             {
27                 lock (this)
28                 {
29                     strLine = sr.ReadLine();
30                     for(int i = 1;i<51;i++)
31                     {
32                         while (strLine != null)
33                         {
34                             Console.WriteLine("读出数据:{0}", strLine);
35                             strLine = sr.ReadLine();
36                         }
37                     }
38                     sr.Close();
39                     Fs.Close();
40                     //Monitor.Wait(this);
41                     Monitor.PulseAll(this);
42                     
43                     WriteToTxt w = new WriteToTxt();
44                     w.write(51);
45                 }
46             }
47             catch (Exception e)
48             {
49                 Console.WriteLine("Read_ERROR:"+e.Message);
50             }
51         }
52     }
53 }
复制代码

 

posted @   strucoder  阅读(211)  评论(0编辑  收藏  举报
编辑推荐:
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
阅读排行:
· BotSharp + MCP 三步实现智能体开发
· BotSharp 5.0 MCP:迈向更开放的AI Agent框架
· 在线聊天系统中的多窗口数据同步技术解密
· 分享 3 款基于 .NET 开源且免费的远程桌面工具
· 5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
点击右上角即可分享
微信分享提示