多线程编程小试
Test1

 MyThread
MyThread
1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 namespace multithreadedprograming
namespace multithreadedprograming
6

 {
{
7 public class MyThread
    public class MyThread
8
 
     {
{
9 public static void Thread1()
        public static void Thread1()
10
 
         {
{
11 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)
12
 
             {
{
13 Console.WriteLine("Thread1 {0}", i);
                Console.WriteLine("Thread1 {0}", i);
14 }
            }
15 }
        }
16
17 public static void Thread2()
        public static void Thread2()
18
 
         {
{
19 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)
20
 
             {
{
21 Console.WriteLine("Thread2 {0}", i);
                Console.WriteLine("Thread2 {0}", i);
22 }
            }
23 }
        }
24 }
    }
25 }
}

 Main
Main
1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 using System.Threading;
using System.Threading;
6
7 namespace multithreadedprograming
namespace multithreadedprograming
8

 {
{
9 class Program
    class Program
10
 
     {
{
11 static void Main(string[] args)
        static void Main(string[] args)
12
 
         {
{
13 Console.WriteLine("Before start thread");
            Console.WriteLine("Before start thread");
14
15 Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1));
            Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1));
16 Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2));
            Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2));
17
18 tid1.Start();
            tid1.Start();
19 tid2.Start();
            tid2.Start();
20
21 Console.ReadKey();
            Console.ReadKey();
22 }
        }
23 }
    }
24 }
}

test2

 MyThread
MyThread
1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 using System.Threading;
using System.Threading;
6
7 namespace multithreadedprograming
namespace multithreadedprograming
8

 {
{
9 public class MyThread
    public class MyThread
10
 
     {
{
11 public void Thread1()
        public void Thread1()
12
 
         {
{
13 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)
14
 
             {
{
15
16 int iHour = 0;
                int iHour = 0;
17 int iMin = 0;
                int iMin = 0;
18 int iSec = 1;
                int iSec = 1;
19
20 Console.WriteLine("Hello world " + i);
                Console.WriteLine("Hello world " + i);
21 Thread.Sleep(new TimeSpan(iHour, iMin, iSec));
                Thread.Sleep(new TimeSpan(iHour, iMin, iSec));
22 }
            }
23 }
        }
24 }
    }
25 }
}

Test3

 MyThread
MyThread
1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 using System.Threading;
using System.Threading;
6 using System.Security;
using System.Security;
7
8 namespace multithreadedprograming
namespace multithreadedprograming
9

 {
{
10 public class MyThread
    public class MyThread
11
 
     {
{
12 public void Thread1()
        public void Thread1()
13
 
         {
{
14 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)
15
 
             {
{
16
17 Thread thr = Thread.CurrentThread;
                Thread thr = Thread.CurrentThread;
18 Console.WriteLine(thr.Name + "=" + i);
                Console.WriteLine(thr.Name + "=" + i);
19
20 try
                try
21
 
                 {
{
22 Thread.Sleep(1);
                    Thread.Sleep(1);
23 }
                }
24 catch (ArgumentException ae)
                catch (ArgumentException ae)
25
 
                 {
{
26 Console.WriteLine(ae.ToString());
                    Console.WriteLine(ae.ToString());
27 }
                }
28 catch (ThreadInterruptedException tie)
                catch (ThreadInterruptedException tie)
29
 
                 {
{
30 Console.WriteLine(tie.ToString());
                    Console.WriteLine(tie.ToString());
31 }
                }
32 catch (SecurityException se)
                catch (SecurityException se)
33
 
                 {
{
34 Console.WriteLine(se.ToString());
                    Console.WriteLine(se.ToString());
35 }
                }
36 }
            }
37 }
        }
38 }
    }
39 }
}

 Main
Main
1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 using System.Threading;
using System.Threading;
6
7 namespace multithreadedprograming
namespace multithreadedprograming
8

 {
{
9 class Program
    class Program
10
 
     {
{
11 static void Main(string[] args)
        static void Main(string[] args)
12
 
         {
{
13 Console.WriteLine("Before start thread");
         Console.WriteLine("Before start thread");
14
15 MyThread thr1 = new MyThread();
                MyThread thr1 = new MyThread();
16 MyThread thr2 = new MyThread();
                MyThread thr2 = new MyThread();
17
18 Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
19 Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
20
21 tid1.Name = "Thread 1";
                tid1.Name = "Thread 1";
22 tid2.Name = "Thread 2";
                tid2.Name = "Thread 2";
23
24
 try
                try  {
{
25 tid1.Start();
                        tid1.Start();
26 tid2.Start();
                        tid2.Start();
27 }
                }
28
 catch (ThreadStateException te)
                catch (ThreadStateException te)  {
{
29 Console.WriteLine(te.ToString() );
                        Console.WriteLine(te.ToString() );
30 }
                }
31
32 tid1.Join();//加入当前线程,直到当前线程结束才也结束。
                tid1.Join();//加入当前线程,直到当前线程结束才也结束。
33 //tid2.Join();//不加入当前线程
                //tid2.Join();//不加入当前线程
34
35 //Thread.Sleep(10);
                //Thread.Sleep(10);
36
37 Console.WriteLine("End of Main");
                Console.WriteLine("End of Main");
38
39 Console.ReadKey();
            Console.ReadKey();
40 }
        }
41 }
    }
42 }
}

Test4 ManualResetEvent

 MyThread
MyThread
1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 using System.Threading;
using System.Threading;
6 using System.Security;
using System.Security;
7
8 namespace multithreadedprograming
namespace multithreadedprograming
9

 {
{
10 public class MyThread
    public class MyThread
11
 
     {
{
12 ManualResetEvent mre = new ManualResetEvent(false);
        ManualResetEvent mre = new ManualResetEvent(false);
13
14 public ManualResetEvent MRE
        public ManualResetEvent MRE
15
 
         {
{
16 get
            get
17
 
             {
{
18 return mre;
                return mre;
19 }
            }
20 }
        }
21
22 public void Thread1()
        public void Thread1()
23
 
         {
{
24 mre.WaitOne();
            mre.WaitOne();
25
26 Console.WriteLine("Receive a envent");
            Console.WriteLine("Receive a envent");
27
28 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)
29
 
             {
{
30
31 Thread thr = Thread.CurrentThread;
                Thread thr = Thread.CurrentThread;
32 Console.WriteLine(thr.Name + "=" + i);
                Console.WriteLine(thr.Name + "=" + i);
33
34 try
                try
35
 
                 {
{
36 Thread.Sleep(1);
                    Thread.Sleep(1);
37 }
                }
38 catch (ArgumentException ae)
                catch (ArgumentException ae)
39
 
                 {
{
40 Console.WriteLine(ae.ToString());
                    Console.WriteLine(ae.ToString());
41 }
                }
42 catch (ThreadInterruptedException tie)
                catch (ThreadInterruptedException tie)
43
 
                 {
{
44 Console.WriteLine(tie.ToString());
                    Console.WriteLine(tie.ToString());
45 }
                }
46 catch (SecurityException se)
                catch (SecurityException se)
47
 
                 {
{
48 Console.WriteLine(se.ToString());
                    Console.WriteLine(se.ToString());
49 }
                }
50 }
            }
51 }
        }
52 }
    }
53 }
}

 Main
Main
1 using System;
using System;
2 using System.Collections.Generic;
using System.Collections.Generic;
3 using System.Text;
using System.Text;
4
5 using System.Threading;
using System.Threading;
6
7 namespace multithreadedprograming
namespace multithreadedprograming
8

 {
{
9 class Program
    class Program
10
 
     {
{
11 static void Main(string[] args)
        static void Main(string[] args)
12
 
         {
{
13 Console.WriteLine("Before start thread");
            Console.WriteLine("Before start thread");
14
15 MyThread thr1 = new MyThread();
            MyThread thr1 = new MyThread();
16
17 Thread tid1 = new Thread(new ThreadStart(thr1.Thread1));
            Thread tid1 = new Thread(new ThreadStart(thr1.Thread1));
18
19 tid1.Name = "Thread 1";
            tid1.Name = "Thread 1";
20
21 try
            try
22
 
             {
{
23 tid1.Start();
                tid1.Start();
24 }
            }
25 catch (ThreadStateException te)
            catch (ThreadStateException te)
26
 
             {
{
27 Console.WriteLine(te.ToString());
                Console.WriteLine(te.ToString());
28 }
            }
29 Console.WriteLine("Main Thread is live? {0}", Thread.CurrentThread.IsAlive);
            Console.WriteLine("Main Thread is live? {0}", Thread.CurrentThread.IsAlive);
30
31 Console.WriteLine("End of Main");
            Console.WriteLine("End of Main");
32
33 thr1.MRE.Set();
            thr1.MRE.Set();
34
35 Console.ReadKey();
            Console.ReadKey();
36 }
        }
37 }
    }
38 }
}

 MyThread
MyThread1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 namespace multithreadedprograming
namespace multithreadedprograming6


 {
{7
 public class MyThread
    public class MyThread8

 
     {
{9
 public static void Thread1()
        public static void Thread1()10

 
         {
{11
 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)12

 
             {
{13
 Console.WriteLine("Thread1 {0}", i);
                Console.WriteLine("Thread1 {0}", i);14
 }
            }15
 }
        }16

17
 public static void Thread2()
        public static void Thread2()18

 
         {
{19
 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)20

 
             {
{21
 Console.WriteLine("Thread2 {0}", i);
                Console.WriteLine("Thread2 {0}", i);22
 }
            }23
 }
        }24
 }
    }25
 }
}
 Main
Main1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 using System.Threading;
using System.Threading;6

7
 namespace multithreadedprograming
namespace multithreadedprograming8


 {
{9
 class Program
    class Program10

 
     {
{11
 static void Main(string[] args)
        static void Main(string[] args)12

 
         {
{13
 Console.WriteLine("Before start thread");
            Console.WriteLine("Before start thread");14

15
 Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1));
            Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1));16
 Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2));
            Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2));17

18
 tid1.Start();
            tid1.Start();19
 tid2.Start();
            tid2.Start();20

21
 Console.ReadKey();
            Console.ReadKey();22
 }
        }23
 }
    }24
 }
}test2

 MyThread
MyThread1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 using System.Threading;
using System.Threading;6

7
 namespace multithreadedprograming
namespace multithreadedprograming8


 {
{9
 public class MyThread
    public class MyThread10

 
     {
{11
 public void Thread1()
        public void Thread1()12

 
         {
{13
 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)14

 
             {
{15

16
 int iHour = 0;
                int iHour = 0;17
 int iMin = 0;
                int iMin = 0;18
 int iSec = 1;
                int iSec = 1;19

20
 Console.WriteLine("Hello world " + i);
                Console.WriteLine("Hello world " + i);21
 Thread.Sleep(new TimeSpan(iHour, iMin, iSec));
                Thread.Sleep(new TimeSpan(iHour, iMin, iSec));22
 }
            }23
 }
        }24
 }
    }25
 }
}Test3

 MyThread
MyThread1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 using System.Threading;
using System.Threading;6
 using System.Security;
using System.Security;7

8
 namespace multithreadedprograming
namespace multithreadedprograming9


 {
{10
 public class MyThread
    public class MyThread11

 
     {
{12
 public void Thread1()
        public void Thread1()13

 
         {
{14
 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)15

 
             {
{16

17
 Thread thr = Thread.CurrentThread;
                Thread thr = Thread.CurrentThread;18
 Console.WriteLine(thr.Name + "=" + i);
                Console.WriteLine(thr.Name + "=" + i);19

20
 try
                try21

 
                 {
{22
 Thread.Sleep(1);
                    Thread.Sleep(1);23
 }
                }24
 catch (ArgumentException ae)
                catch (ArgumentException ae)25

 
                 {
{26
 Console.WriteLine(ae.ToString());
                    Console.WriteLine(ae.ToString());27
 }
                }28
 catch (ThreadInterruptedException tie)
                catch (ThreadInterruptedException tie)29

 
                 {
{30
 Console.WriteLine(tie.ToString());
                    Console.WriteLine(tie.ToString());31
 }
                }32
 catch (SecurityException se)
                catch (SecurityException se)33

 
                 {
{34
 Console.WriteLine(se.ToString());
                    Console.WriteLine(se.ToString());35
 }
                }36
 }
            }37
 }
        }38
 }
    }39
 }
}
 Main
Main1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 using System.Threading;
using System.Threading;6

7
 namespace multithreadedprograming
namespace multithreadedprograming8


 {
{9
 class Program
    class Program10

 
     {
{11
 static void Main(string[] args)
        static void Main(string[] args)12

 
         {
{13
 Console.WriteLine("Before start thread");
         Console.WriteLine("Before start thread");14

15
 MyThread thr1 = new MyThread();
                MyThread thr1 = new MyThread();16
 MyThread thr2 = new MyThread();
                MyThread thr2 = new MyThread();17

18
 Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );
                Thread tid1 = new Thread(new ThreadStart(thr1.Thread1) );19
 Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );
                Thread tid2 = new Thread(new ThreadStart(thr2.Thread1) );20

21
 tid1.Name = "Thread 1";
                tid1.Name = "Thread 1";22
 tid2.Name = "Thread 2";
                tid2.Name = "Thread 2";23

24

 try
                try  {
{25
 tid1.Start();
                        tid1.Start();26
 tid2.Start();
                        tid2.Start();27
 }
                }28

 catch (ThreadStateException te)
                catch (ThreadStateException te)  {
{29
 Console.WriteLine(te.ToString() );
                        Console.WriteLine(te.ToString() );30
 }
                }31

32
 tid1.Join();//加入当前线程,直到当前线程结束才也结束。
                tid1.Join();//加入当前线程,直到当前线程结束才也结束。33
 //tid2.Join();//不加入当前线程
                //tid2.Join();//不加入当前线程34

35
 //Thread.Sleep(10);
                //Thread.Sleep(10);36

37
 Console.WriteLine("End of Main");
                Console.WriteLine("End of Main");38

39
 Console.ReadKey();
            Console.ReadKey();40
 }
        }41
 }
    }42
 }
}Test4 ManualResetEvent

 MyThread
MyThread1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 using System.Threading;
using System.Threading;6
 using System.Security;
using System.Security;7

8
 namespace multithreadedprograming
namespace multithreadedprograming9


 {
{10
 public class MyThread
    public class MyThread11

 
     {
{12
 ManualResetEvent mre = new ManualResetEvent(false);
        ManualResetEvent mre = new ManualResetEvent(false);13

14
 public ManualResetEvent MRE
        public ManualResetEvent MRE15

 
         {
{16
 get
            get17

 
             {
{18
 return mre;
                return mre;19
 }
            }20
 }
        }21

22
 public void Thread1()
        public void Thread1()23

 
         {
{24
 mre.WaitOne();
            mre.WaitOne();25

26
 Console.WriteLine("Receive a envent");
            Console.WriteLine("Receive a envent");27

28
 for (int i = 0; i < 10; i++)
            for (int i = 0; i < 10; i++)29

 
             {
{30

31
 Thread thr = Thread.CurrentThread;
                Thread thr = Thread.CurrentThread;32
 Console.WriteLine(thr.Name + "=" + i);
                Console.WriteLine(thr.Name + "=" + i);33

34
 try
                try35

 
                 {
{36
 Thread.Sleep(1);
                    Thread.Sleep(1);37
 }
                }38
 catch (ArgumentException ae)
                catch (ArgumentException ae)39

 
                 {
{40
 Console.WriteLine(ae.ToString());
                    Console.WriteLine(ae.ToString());41
 }
                }42
 catch (ThreadInterruptedException tie)
                catch (ThreadInterruptedException tie)43

 
                 {
{44
 Console.WriteLine(tie.ToString());
                    Console.WriteLine(tie.ToString());45
 }
                }46
 catch (SecurityException se)
                catch (SecurityException se)47

 
                 {
{48
 Console.WriteLine(se.ToString());
                    Console.WriteLine(se.ToString());49
 }
                }50
 }
            }51
 }
        }52
 }
    }53
 }
}
 Main
Main1
 using System;
using System;2
 using System.Collections.Generic;
using System.Collections.Generic;3
 using System.Text;
using System.Text;4

5
 using System.Threading;
using System.Threading;6

7
 namespace multithreadedprograming
namespace multithreadedprograming8


 {
{9
 class Program
    class Program10

 
     {
{11
 static void Main(string[] args)
        static void Main(string[] args)12

 
         {
{13
 Console.WriteLine("Before start thread");
            Console.WriteLine("Before start thread");14

15
 MyThread thr1 = new MyThread();
            MyThread thr1 = new MyThread();16

17
 Thread tid1 = new Thread(new ThreadStart(thr1.Thread1));
            Thread tid1 = new Thread(new ThreadStart(thr1.Thread1));18

19
 tid1.Name = "Thread 1";
            tid1.Name = "Thread 1";20

21
 try
            try22

 
             {
{23
 tid1.Start();
                tid1.Start();24
 }
            }25
 catch (ThreadStateException te)
            catch (ThreadStateException te)26

 
             {
{27
 Console.WriteLine(te.ToString());
                Console.WriteLine(te.ToString());28
 }
            }29
 Console.WriteLine("Main Thread is live? {0}", Thread.CurrentThread.IsAlive);
            Console.WriteLine("Main Thread is live? {0}", Thread.CurrentThread.IsAlive);30

31
 Console.WriteLine("End of Main");
            Console.WriteLine("End of Main");32

33
 thr1.MRE.Set();
            thr1.MRE.Set();34

35
 Console.ReadKey();
            Console.ReadKey();36
 }
        }37
 }
    }38
 }
}.jpg) 
  
 
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号