多线程编程小试
Test1

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

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

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

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

test2

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

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

Test3

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

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

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

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

Test4 ManualResetEvent

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

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

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

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

5
namespace multithreadedprograming6


{7
public class MyThread8

{9
public static void Thread1()10

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

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

17
public static void Thread2()18

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

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

5
using System.Threading;6

7
namespace multithreadedprograming8


{9
class Program10

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

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

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

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

21
Console.ReadKey();22
}23
}24
}test2
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
using System.Threading;6

7
namespace multithreadedprograming8


{9
public class MyThread10

{11
public void Thread1()12

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

{15

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

20
Console.WriteLine("Hello world " + i);21
Thread.Sleep(new TimeSpan(iHour, iMin, iSec));22
}23
}24
}25
}Test3
1
using System;2
using System.Collections.Generic;3
using System.Text;4

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

8
namespace multithreadedprograming9


{10
public class MyThread11

{12
public void Thread1()13

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

{16

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

20
try21

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

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

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

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

5
using System.Threading;6

7
namespace multithreadedprograming8


{9
class Program10

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

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

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

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

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

24

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

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

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

35
//Thread.Sleep(10);36

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

39
Console.ReadKey();40
}41
}42
}Test4 ManualResetEvent
1
using System;2
using System.Collections.Generic;3
using System.Text;4

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

8
namespace multithreadedprograming9


{10
public class MyThread11

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

14
public ManualResetEvent MRE15

{16
get17

{18
return mre;19
}20
}21

22
public void Thread1()23

{24
mre.WaitOne();25

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

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

{30

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

34
try35

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

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

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

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

5
using System.Threading;6

7
namespace multithreadedprograming8


{9
class Program10

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

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

15
MyThread thr1 = new MyThread();16

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

19
tid1.Name = "Thread 1";20

21
try22

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

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

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

33
thr1.MRE.Set();34

35
Console.ReadKey();36
}37
}38
}

浙公网安备 33010602011771号