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

 如需转载,注明: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 }

 

//第二个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 @ 2013-10-17 01:46  strucoder  阅读(210)  评论(0编辑  收藏  举报