5.2
CurrentThread | - ( ) | |
IsAlive | true false - | |
IsBackground | , , | |
Name | ||
Priority | / ( ThreadPriority | |
ThreadState | (- ThreadState) | |
Abort | ThreadAbor- tException. | |
GetData, SetData | () | |
GetDomain, GetDo- mainID | ( ), | |
GetHashCode | - | |
Sleep | - | |
Start | , - ThreadStart | |
Suspend | . - - , | |
Resume | ||
Join | - - | |
Interrupt |
- , - , :
Thread t = new Thread (new ThreadStart (_)); , . 5.1
.
5.1.
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static public void Hedgehog()
//
{
for(inti= 0; i< 6; ++i)
{
Console.WriteLine(i);Thread.Sleep(1000);
}
}
static void Main()
{
Console.WriteLine(" " +
Thread.CurrentThread.GetHashCode());
Thread ta = new Thread(newThreadStart(Hedgehog)); Console.WriteLine(" " +
ta.GetHashCode());
ta.Start();
for (int i = 0; i > -6; --i)
{
Console.Write(" " +i);Thread.Sleep(400);
}
}
}
}
,
. . , ( 5.2).
|
|
5.2. ,
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Class1
{
public void Do()
{
for (int i = 0; i < 4; ++i)
{
Console.Write(" " + i); Thread.Sleep(3);
}
}
}
class Program
{
static void Main()
{
Class1 a = new Class1(); Thread t1=new Thread (new
ThreadStart(a.Do));
t1.Name ="Second"; Console.WriteLine(""+t1.Name); t1.Start();
Thread t2=new Thread (new
ThreadStart(a.Do)); t2.Name="Third"; Console.WriteLine(""+t2.Name); t2.Start();
}
}
}
, lock, :
lock () _
, .
this, typeof(). - , .
5.3.
, . - . - :
1. | 1 2 | 6. 2 13 | 11. 2 11 |
2. | 1 3 | 7. 12 13 | 12. 10 11 |
3. | 1 12 | 8. 1 14 | 13. 10 12 |
4. | 2 12 | 9. 2 15 | 14. 1 12 |
5. 1 13 10. 1 11 15. 7 13
5.3
5.3