-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadForExam.cs
99 lines (93 loc) · 2.96 KB
/
ThreadForExam.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace OOPs
{
class ThreadForExam
{
public static void Method1()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
Thread.Sleep(1000);
}
}
//Paramertrised Thread
public static void Method2(object num)
{
for (int i = 0; i < (int)num; i++)
{
Console.WriteLine(i);
Thread.Sleep(100);
}
}
[ThreadStatic]
public static int _ThreadStaticField;
public static int _NormalField;
public static void testThreadStatic()
{
new Thread(() =>
{
for (int i = 0; i < 10; i++)
{
_ThreadStaticField++;
_NormalField++;
Console.WriteLine("Thread 1(_ThreadStaticField) : " + _ThreadStaticField);
Console.WriteLine("Thread 1(_NormalField) : " + _NormalField);
Thread.Sleep(1000);
}
}).Start();
new Thread(() =>
{
for (int i = 0; i < 10; i++)
{
_ThreadStaticField++;
_NormalField++;
Console.WriteLine("Thread 2(_ThreadStaticField) : " + _ThreadStaticField);
Console.WriteLine("Thread 2(_NormalField) : " + _NormalField);
Thread.Sleep(1000);
}
}).Start();
new Thread(() =>
{
for (int i = 0; i < 10; i++)
{
_ThreadStaticField++;
_NormalField++;
Console.WriteLine("Thread 3(_ThreadStaticField) : " + _ThreadStaticField);
Console.WriteLine("Thread 3(_NormalField) : " + _NormalField);
Thread.Sleep(1000);
}
}).Start();
}
public static ThreadLocal<int> _field = new ThreadLocal<int>(() => {
return Thread.CurrentThread.ManagedThreadId;
});
public static void testThreadLocal()
{
new Thread(() => {
for (int i = 0; i < _field.Value; i++)
{
Console.WriteLine("A : " + i);
Thread.Sleep(1000);
}
}).Start();
new Thread(() =>
{
for (int i = 0; i < _field.Value; i++)
{
Console.WriteLine("B : " + i);
Thread.Sleep(1000);
}
}).Start();
}
static void Main()
{
testThreadLocal();
Console.ReadKey();
}
}
}