Csharp/CSharp Tutorial/Thread/Thread Priority

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Thread priorities.

using System; 
using System.Threading; 
 
class MyThread { 
  public int count; 
  public Thread thrd; 
 
  public MyThread(string name) { 
    count = 0; 
    thrd = new Thread(this.run); 
    thrd.Name = name; 
  } 
 
  void run() { 
    Console.WriteLine(thrd.Name + " starting."); 
    do { 
      count++; 
 
        Console.WriteLine("In " + thrd.Name); 
    } while(count < 10000); 
 
    Console.WriteLine(thrd.Name + " terminating."); 
  } 
} 
 
class PriorityDemo { 
  public static void Main() { 
    MyThread mt1 = new MyThread("High Priority"); 
    MyThread mt2 = new MyThread("Low Priority"); 
 
    mt1.thrd.Priority = ThreadPriority.AboveNormal; 
    mt2.thrd.Priority = ThreadPriority.BelowNormal; 
 
    mt1.thrd.Start(); 
    mt2.thrd.Start(); 
 
    mt1.thrd.Join(); 
    mt2.thrd.Join(); 
 
    Console.WriteLine(); 
    Console.WriteLine(mt1.thrd.Name + " thread counted to " + mt1.count); 
    Console.WriteLine(mt2.thrd.Name + " thread counted to " + mt2.count); 
  } 
}
High Priority starting.
In High Priority
In High Priority
In High Priority
In High Priority
...
^CTerminate batch job (Y/N)? n

Thread priorities: ThreadPriority.Highest, ThreadPriority.Lowest

using System;
using System.Threading;
class MainClass
{
  public static void Countdown() 
  {
    for (int i = 10; i > 0; i--) 
    {
      Console.Write(i.ToString() + " ");
    }
  }
  public static void Main() 
  {
    Thread t2 = new Thread(new ThreadStart(Countdown));
    t2.Priority=ThreadPriority.Highest;
    Thread.CurrentThread.Priority=ThreadPriority.Lowest;
    t2.Start();
  }
}
10 9 8 7 6 5 4 3 2 1

User Thread: Name, ApartmentState, IsAlive, Priority, ThreadState

using System;
using System.Threading;
class MainClass
{
  static void MyThreadProc()
  {
    Thread.CurrentThread.Name = "TheSecondaryThread";
    Thread secondaryThread = Thread.CurrentThread;
    Console.WriteLine("Name? {0}", secondaryThread.Name);
    Console.WriteLine("Alive? {0}", secondaryThread.IsAlive);
    Console.WriteLine("Priority? {0}", secondaryThread.Priority);      
    Console.WriteLine("State? {0}", secondaryThread.ThreadState);
    Console.WriteLine();
    for(int i = 0; i < 1000; i ++)
    {
      Console.WriteLine("Value of i is: {0}", i);
      Thread.Sleep(5);
    }
  }
  [MTAThread]
  static void Main(string[] args)
  {
    // Start a new worker thread.
    Thread secondaryThread = new Thread(new ThreadStart(MyThreadProc));
    secondaryThread.Priority = ThreadPriority.Highest;
    secondaryThread.Start();
  }
}
Name? TheSecondaryThread
Alive? True
Priority? Highest
State? Running
Value of i is: 0
Value of i is: 1
Value of i is: 2
Value of i is: 3
Value of i is: 4
Value of i is: 5
Value of i is: 6
Value of i is: 7
Value of i is: 8
Value of i is: 9
Value of i is: 10
Value of i is: 11
Value of i is: 12
Value of i is: 13
Value of i is: 14
Value of i is: 15
Value of i is: 16
Value of i is: 17
Value of i is: 18
Value of i is: 19
Value of i is: 20
Value of i is: 21
Value of i is: 22
Value of i is: 23
Value of i is: 24
Value of i is: 25
Value of i is: 26
Value of i is: 27
Value of i is: 28
Value of i is: 29
Value of i is: 30
Value of i is: 31
Value of i is: 32
Value of i is: 33
Value of i is: 34
Value of i is: 35
Value of i is: 36
Value of i is: 37
Value of i is: 38
Value of i is: 39
Value of i is: 40
Value of i is: 41
Value of i is: 42
Value of i is: 43
Value of i is: 44
Value of i is: 45
Value of i is: 46
Value of i is: 47
Value of i is: 48
Value of i is: 49
Value of i is: 50
Value of i is: 51
Value of i is: 52
Value of i is: 53
Value of i is: 54
Value of i is: 55
Value of i is: 56
Value of i is: 57
Value of i is: 58
Value of i is: 59
Value of i is: 60
Value of i is: 61
Value of i is: 62
Value of i is: 63
Value of i is: 64
Value of i is: 65
Value of i is: 66
Value of i is: 67
Value of i is: 68
Value of i is: 69
Value of i is: 70
Value of i is: 71
Value of i is: 72
Value of i is: 73
Value of i is: 74
Value of i is: 75
Value of i is: 76
Value of i is: 77
Value of i is: 78
Value of i is: 79
Value of i is: 80
Value of i is: 81
Value of i is: 82
Value of i is: 83
Value of i is: 84
Value of i is: 85
Value of i is: 86
Value of i is: 87
Value of i is: 88
Value of i is: 89
Value of i is: 90
Value of i is: 91
Value of i is: 92
Value of i is: 93
Value of i is: 94
Value of i is: 95
Value of i is: 96
Value of i is: 97
Value of i is: 98
Value of i is: 99
Value of i is: 100
Value of i is: 101
Value of i is: 102
Value of i is: 103
Value of i is: 104
Value of i is: 105
Value of i is: 106
Value of i is: 107
Value of i is: 108
Value of i is: 109
Value of i is: 110
Value of i is: 111
Value of i is: 112
Value of i is: 113
Value of i is: 114
Value of i is: 115
Value of i is: 116
Value of i is: 117
Value of i is: 118
Value of i is: 119
Value of i is: 120
Value of i is: 121
Value of i is: 122
Value of i is: 123
Value of i is: 124
Value of i is: 125
Value of i is: 126
Value of i is: 127
Value of i is: 128
Value of i is: 129
Value of i is: 130
Value of i is: 131
Value of i is: 132
Value of i is: 133
Value of i is: 134
Value of i is: 135
Value of i is: 136
Value of i is: 137
Value of i is: 138
Value of i is: 139
Value of i is: 140
Value of i is: 141
Value of i is: 142
Value of i is: 143
Value of i is: 144
Value of i is: 145
Value of i is: 146
Value of i is: 147
Value of i is: 148
Value of i is: 149
Value of i is: 150
Value of i is: 151
Value of i is: 152
Value of i is: 153
Value of i is: 154
Value of i is: 155
Value of i is: 156
Value of i is: 157
Value of i is: 158
Value of i is: 159
Value of i is: 160
Value of i is: 161
Value of i is: 162
Value of i is: 163
Value of i is: 164
Value of i is: 165
Value of i is: 166
Value of i is: 167
Value of i is: 168
Value of i is: 169
Value of i is: 170
Value of i is: 171
Value of i is: 172
Value of i is: 173
Value of i is: 174
Value of i is: 175
Value of i is: 176
Value of i is: 177
Value of i is: 178
Value of i is: 179
Value of i is: 180
Value of i is: 181
Value of i is: 182
Value of i is: 183
Value of i is: 184
Value of i is: 185
Value of i is: 186
Value of i is: 187
Value of i is: 188
Value of i is: 189
Value of i is: 190
Value of i is: 191
Value of i is: 192
Value of i is: 193
Value of i is: 194
Value of i is: 195
Value of i is: 196
Value of i is: 197
Value of i is: 198
Value of i is: 199
Value of i is: 200
Value of i is: 201
Value of i is: 202
Value of i is: 203
Value of i is: 204
Value of i is: 205
Value of i is: 206
Value of i is: 207
Value of i is: 208
Value of i is: 209
Value of i is: 210
Value of i is: 211
Value of i is: 212
Value of i is: 213
Value of i is: 214
Value of i is: 215
Value of i is: 216
Value of i is: 217
Value of i is: 218
Value of i is: 219
Value of i is: 220
Value of i is: 221
Value of i is: 222
Value of i is: 223
Value of i is: 224
Value of i is: 225
Value of i is: 226
Value of i is: 227
Value of i is: 228
Value of i is: 229
Value of i is: 230
Value of i is: 231
Value of i is: 232
Value of i is: 233
Value of i is: 234
Value of i is: 235
Value of i is: 236
Value of i is: 237
Value of i is: 238
Value of i is: 239
Value of i is: 240
Value of i is: 241
Value of i is: 242
Value of i is: 243
Value of i is: 244
Value of i is: 245
Value of i is: 246
Value of i is: 247
Value of i is: 248
Value of i is: 249
Value of i is: 250
Value of i is: 251
Value of i is: 252
Value of i is: 253
Value of i is: 254
Value of i is: 255
Value of i is: 256
Value of i is: 257
Value of i is: 258
Value of i is: 259
Value of i is: 260
Value of i is: 261
Value of i is: 262
Value of i is: 263
Value of i is: 264
Value of i is: 265
Value of i is: 266
Value of i is: 267
Value of i is: 268
Value of i is: 269
Value of i is: 270
Value of i is: 271
Value of i is: 272
Value of i is: 273
Value of i is: 274
Value of i is: 275
Value of i is: 276
Value of i is: 277
^CTerminate batch job (Y/N)? n