Introduction to 'NET Platform and 'NET Framework - PowerPoint PPT Presentation

1 / 195
About This Presentation
Title:

Introduction to 'NET Platform and 'NET Framework

Description:

Console.WriteLine('Thread1:job({0})',i); public void doTask2() { for ... Thread1:job(98) Thread1:job(99) ???????????? #2. Small Example. ????? ?? ???????. Start ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 196
Provided by: sve84
Category:

less

Transcript and Presenter's Notes

Title: Introduction to 'NET Platform and 'NET Framework


1
(No Transcript)
2
???????????? ???????????? ? ?????????????
???????????? ?? .NET Framework
http//www.nakov.com/dotnet-project/
?????? ???????
???-????? www.gis-sofia.bg Microsoft Student
Consultant www.fmi.uni-sofia.bg/microsoft
3
?????????? ??????
  • ?????? ???????? ?? .NET Framework ? CLR
  • ?????? ???????? ?? ?????? ??????? ?? ?????? ?
    .NET (Common Type System)
  • ?????? ???????? ?? ????? C
  • ?????? ???????? ?? ??????????? ???????
  • Attributes

4
??????????
  • ??????????????
  • ?????
  • ?????????????
  • ?????????? ??????????

5
??????????????
  • ??????????????
  • ????????? ???? ??????????????
  • ??????? ?? ????????????????
  • ????????? - ??????? ? ?????
  • ??????? ? ???????
  • ????? ?????????
  • ???? ?? ?????? ???????
  • ?????????????? ??????
  • ????????????? ?? ??????????????
  • Application Domains
  • ?????
  • ?????????????
  • ?????????? ??????????

6
?????????
  • ??? ??????, ? ????? ?????? ?????????? ?????? ??
    ??????? ????????????? ???????? ??? ?? ???? ??
    ??????????????? ?? ??????
  • ??? ??????, ? ????? ?????? ?????????? ?????? ??
    ???????? ???????? ?? ????? ????
  • ????? ? ?????????, ????? ?? ????????? ???????
    ???????? ?? ?? ????????? ????????????

7
??????? ?? ????????????????
  • Performance ??? ??? ?? ????? ?????????
  • Responsiveness ????????? ???????? ??????????
    ????? ??? ???????????? ??????
  • Throughput ??????????? ?? ??????????????????
  • ??????? ??? ????????????
  • ?????????? ?? ????? ??????????? ????????????

8
???????????? 1
  • ???? ? ????? ??????????????

9
????????? - ??????? ? ?????
  • ???????? ???????????? ???????????? ?? ?????, ????
    ? ???? ?? ????????????
  • OS ????????? ??????? (process), ?? ??
    ????????????? ?????????? ??????????
  • ??????? (thread) ? ????????? ???????, ?? ?????
    ???? ?? ?? ?????? ?????????? ?????
  • ? ???? ?????? ?????????? ?????? ?? ?????????? ??
    ??????? ?????
  • ? ???? ?????? ??? ???? ???? ?????

10
?????? vs. ?????
  • ???????
  • ???????? ???? (stack)
  • ?????????
  • exception handlers
  • ???????
  • ????????? ?? ????????? ???? ?? ???? ?? ?????????
    ?? ????? ? ?????
  • ??????? ? ???? ?????? ???????? ???????,
    ???????????? ? ??????? ?? ???? ??????
  • ????????? ???????? ?????????? ???, ? ??????? ??
    ??????????

11
????? ????????? ????????
  • ???????????? ?? ??????? ?????
  • ??????? ??????????? ?? ???????????? ?? ???????
    ?????? ????????????
  • ????????? ? ???????, ???? ????? ???? ????? ?????
    ????????? - ????? ??????
  • ????????
  • ???????????? ???? ?? ??????? ????? ?
    ????????????????? ???????? ?????? ?? ???????
    ????? ?? ???????
  • ?? ???? ??????????? ??? ???????????? ???????? ??
    ?????? ????????? ?? ?????? ???????? ??

12
???? ?? ?????? ????????
  • ??????, ? ????? ? ?????? ?? ?? ????????? ?????
  • ?????????? ?? ????? ??????????? ????????????,
    ???????? Web ??????
  • ??????????? ???? ????? (sockets)
  • ?????????? ?? ????????????? ????????
  • ??????????? ?? ???????? ?? ?????????
  • ?? ?? ???? ??????????????? ????????? ?? ????????
    ?? ???????? ?? ????????????? ??????, ?????? ??
    ????? ???? ?? ???????? ????? ??????

13
?????????????? - ???????????? ? ????????????
  • ???????????? ?????????????? (cooperative
    multitasking)
  • ??????? ???? ?????? ????? ?????????? ????? ? ?
    ??????????
  • ????? ?? ?? ????????? ???? ??? ? ???????? ???
    ???? ?? ??????? ??????
  • ???????????? ?????????????? (preemptive
    multitasking)
  • ????????????? (task scheduler) ??????
    ????????????? ?????????? ?????????? ?????
  • ??? ???????? ???? ? ??????????, ??????? ?? ?????
    ?? ?????????

14
?????????????? - ???????????? ? ????????????
  • ??? ?????????????? ??????????????
  • ???? ????? ???? ????? ????? ?? ????? ?????????
  • ?????? ??? ?????????? ????? ????? ?? ?????
    ??????????? ????? ?????
  • ????? ??????? ????????? ?????????? ???????
  • ????? ? ????? ????????? ?? ?????? ????????????
    ?????? ??????? ? ??-????? ?????????

15
????????????? ?? ??????????????
Apartment
Apartment
Process
Global Data
Global Data
Global Data
Thread
Thread
Thread
Thread
Instruction
Instruction
Instruction
Instruction
Instruction
Instruction
Instruction
Instruction
Apartment Threading
Free Threading
16
Application Domain
  • .NET ?????? ??? ???? ???? ?? ?????????? ?????
    ????? ? ?????? Application Domain
  • ????????? ???????? ??? ??????? ? ?????????, ???
    ???? ? ????????????? ?? ???? OS
  • ??????????
  • ????????? ????????????????
  • ???? ?? ???????? - ???? ??? ???????
  • ?????? ?/? ????? ??? proxy
  • ???????? type checking
  • System.AppDomain

17
System.AppDomain
AppDomain 1
AppDomain 2
AppDomain 3
Global Data
Global Data
Thread
Thread
Instruction
Instruction
AppDomain 4
Thread
Thread
Global Data
Instruction
Instruction
18
?????
  • ??????????????
  • ?????
  • ??? ??????? ???????
  • ????? ?? ???????
  • ??-????? ???????
  • ?????????
  • ?????????
  • Thread Local Storage
  • Thread-Relative Static Fields
  • ????????????
  • ??????????
  • ?????????? ?? ?????
  • ?????????????
  • ?????????? ??????????

19
??? ?????? ????????????????
Time Slice
Running Queue
Instruction
Thread
Pull From Queue
Instruction
Thread
Instruction
Thread
Instruction
Thread
Interrupt
Thread
Return to Queue
20
????? ??????
class FirstThread public void doTask1()
for( int i0 ilt100 i )
Console.WriteLine("Thread1job(0)",i)
public void doTask2() for( int i0 ilt100
i ) Console.WriteLine("Thread2job(0)",i
) class Starter static void
Main(string args) FirstThread ft new
FirstThread() Thread t1 new Thread(
new ThreadStart(ft.doTask1)) Thread t2 new
Thread( new ThreadStart(ft.doTask2))
t1.Start() t2.Start()
21
????? ??????
class FirstThread public void doTask1()
for( int i0 ilt100 i )
Console.WriteLine("Thread1job(0)",i)
public void doTask2() for( int i0 ilt100
i ) Console.WriteLine("Thread2job(0)",i
) class Starter static void
Main(string args) FirstThread ft new
FirstThread() Thread t1 new Thread(
new ThreadStart(ft.doTask1)) Thread t2 new
Thread( new ThreadStart(ft.doTask2))
t1.Start() t2.Start()
22
????? ??????
class FirstThread public void doTask1()
for( int i0 ilt100 i )
Console.WriteLine("Thread1job(0)",i)
public void doTask2() for( int i0 ilt100
i ) Console.WriteLine("Thread2job(0)",i
) class Starter static void
Main(string args) FirstThread ft new
FirstThread() Thread t1 new Thread(
new ThreadStart(ft.doTask1)) Thread t2 new
Thread( new ThreadStart(ft.doTask2))
t1.Start() t2.Start()
23
????? ??????
class FirstThread public void doTask1()
for( int i0 ilt100 i )
Console.WriteLine("Thread1job(0)",i)
public void doTask2() for( int i0 ilt100
i ) Console.WriteLine("Thread2job(0)",i
) class Starter static void
Main(string args) FirstThread ft new
FirstThread() Thread t1 new Thread(
new ThreadStart(ft.doTask1)) Thread t2 new
Thread( new ThreadStart(ft.doTask2))
t1.Start() t2.Start()
24
????? ??????
class FirstThread public void doTask1()
for( int i0 ilt100 i )
Console.WriteLine("Thread1job(0)",i)
public void doTask2() for( int i0 ilt100
i ) Console.WriteLine("Thread2job(0)",i
) class Starter static void
Main(string args) FirstThread ft new
FirstThread() Thread t1 new Thread(
new ThreadStart(ft.doTask1)) Thread t2 new
Thread( new ThreadStart(ft.doTask2))
t1.Start() t2.Start()
25
????? ??????
class FirstThread public void doTask1()
for( int i0 ilt100 i )
Console.WriteLine("Thread1job(0)",i)
public void doTask2() for( int i0 ilt100
i ) Console.WriteLine("Thread2job(0)",i
) class Starter static void
Main(string args) FirstThread ft new
FirstThread() Thread t1 new Thread(
new ThreadStart(ft.doTask1)) Thread t2 new
Thread( new ThreadStart(ft.doTask2))
t1.Start() t2.Start()
26
????? ??????
class FirstThread public void doTask1()
for( int i0 ilt100 i )
Console.WriteLine("Thread1job(0)",i)
public void doTask2() for( int i0 ilt100
i ) Console.WriteLine("Thread2job(0)",i
) class Starter static void
Main(string args) FirstThread ft new
FirstThread() Thread t1 new Thread(
new ThreadStart(ft.doTask1)) Thread t2 new
Thread( new ThreadStart(ft.doTask2))
t1.Start() t2.Start()
27
????? ??????
//???????? Thread1job(1) Thread1job(2) Thread2
job(1) Thread1job(3) ... ... Thread1job(53) Thre
ad2job(46) Thread2job(47) Thread1job(54) ... ..
. Thread1job(97) Thread2job(99) Thread1job(98)
Thread1job(99)
28
???????????? 2
  • Small Example

29
????? ?? ???????
Start()
IsAlive
All Done
Suspend()
Sleep()
WaitX()
Join()
Abort()
Suspended
WaitSleepJoin
WaitSleepJoin
WaitSleepJoin
Stopped
Interrupt()
Interrupt()
Thread Stopped
Aimed Thread Destroyed
Resume()
Expire time
Notified
30
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart )
  • ??????? ?????????
  • ?????? ?? ??????? ? ?????, ????? ?? ?? ???????
    ??? ??????????
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

31
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • ???????? ???????? ????? ?? ???????? ????
    ??????????? (? ???????????)
  • ??????? ?? ???? ?? ?????? ?????
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

32
????? ?????? ?? Sleep()
Instruction
Sleep Queue
Running Queue
T
Thread
Instruction
T
Thread
from Queue
T
Instruction
Thread
To Sleep Queue
T
Instruction
Thread
Sleep()
T
Thread
Clock Interrupt
33
?????????? ?? ?????
using System using System.Threading public
class ThreadSleep public static Thread
worker public static Thread worker2 public
static void Main() Console.WriteLine("Ente
ring void Main!") worker new Thread(new
ThreadStart(Counter)) worker2 new
Thread(new ThreadStart(Counter2))
worker.Start() worker2.Start()
Console.WriteLine("Exiting void Main!")
(???????? ??????????)
34
?????????? ?? ?????
public static void Counter()
Console.WriteLine("Entering Counter")
for(int i 1 i lt 50 i)
Console.Write(i " ") if(i 10)
Thread.Sleep(1000) Console.WriteLine(\
nExiting Counter") public static void
Counter2() Console.WriteLine("Entering
Counter2") for(int i 51 i lt 100 i)
Console.Write(i " ") if( i
70 ) Thread.Sleep(5000)
Console.WriteLine(\nExiting Counter2")
35
???????????? 3
  • SleepExample

36
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • ??? ??????? ? Running, ? ????????????? ????????
    (suspend)
  • ??? ? ?????????????, ?? ?? ?????? ????
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

37
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • ????????? ?????, ????? ? ???? ?????????????
    (suspended)
  • ??? ??????? ??????, ?? ????? ????
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

38
???????????? 4
  • Suspend-Resume

39
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • true, ??? ? ?????????? ? ?? ? ?????? ??????????
    ??? ??????????
  • ?????? ?????????? ???? ThreadState
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

40
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • ?????? ???? (foreground) ? ????? ?????
    (background)
  • ???????? ?? ?????/?????????
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

41
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • ???????? ?? ?????/?????????
  • true, ??? ??????? ?????????? ?? managed thread
    pool, ????? false
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

42
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • ????? ????? ? .NET Framework ???? ?? ??? ???
  • ???????? ?? ?????/?????????
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

43
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • Lowest, BelowNormal, Normal (?? ????????????),
    AboveNormal, Highest
  • ???????? ?? ???????/?????????
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()

44
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • ??????? ??????????? ?? ???????, ???? ?????? ??
    ???? ???? ??????? ??????
  • ???????? ???? ?? ?????????
  • Abort()
  • Interrupt()
  • Join()
  • Start()

45
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • ???? ????? ?????????? ????? ???????
  • ?????? ThreadAbortException ? ?????????? ?????
  • Interrupt()
  • Join()
  • Start()

46
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • ??????? ????? ?? ????????? WaitSleepJoin, ?????
    ?? ????? ????
  • ?????? ThreadInterruptedException
  • Join()
  • Start()

47
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • ??????????? ????? ???????, ?????? ??????????
    ????????
  • ???? ?? ?? ????? ??????? (timeout)
  • Start()

48
???????????? 5
  • Thread.Join()
  • SmallExamplePlusJoin

49
??-??????? ??????? ?? Thread
  • public Thread( ThreadStart start)
  • Sleep()
  • Suspend()
  • Resume()
  • IsAlive
  • IsBackground
  • IsThreadPoolThread
  • Name
  • Priority
  • ThreadState
  • Abort()
  • Interrupt()
  • Join()
  • Start()
  • ???????? ?????????? ?????
  • ??????????? ????????

50
????????? ?? ?????
  • ???????? ????????????? ?? ?????????????? ????????
    ????????????? ???????? ?????????
  • ??????????? ?????? ????? ????? ? ???????? ??
    ???????
  • ????? ? ?? ???????????? (task scheduler)
  • ??????????? ? .NET Framework
  • ???????? ?????????
  • Lowest, BelowNormal, Normal (?? ????????????),
    AboveNormal, Highest
  • ? Win32 ??? ? ????????? RealTime
  • OS ?? ? ?????? ?? ?? ?????????? ? ???????????????
    ?? ???????

51
?????????
class FirstThread public void doTask1()
for( int i0 ilt100 i )
Console.WriteLine("Thread1job(0)",i)
public void doTask2() for( int i0 ilt100
i ) Console.WriteLine("Thread2job(0)",i
) class Starter static void
Main(string args) FirstThread ft new
FirstThread() Thread t1 new Thread(
new ThreadStart(ft.doTask1)) Thread t2 new
Thread( new ThreadStart(ft.doTask2))
t1.Priority ThreadPriority.Highest
t1.Start() t2.Start()
52
?????????
???????? Thread1job(1) Thread1job(2) Thread2jo
b(1) Thread1job(3) ... ... Thread1job(97) Thread
1job(98) Thread2job(85) Thread1job(99) Thread2
job(86) ... ... Thread2job(97) Thread2job(98) Th
read2job(99)
53
ThreadState
  • ????? ????? ??? ????? ???? ?????? ? ? ???? ??
    ??????????? ?? ???????????? ThreadState
  • ??????? ?? ?? ???? ? ??????? ?????????
    ????????????
  • ???????????? ??? ??????? FlagsAttribute
  • ????????? ???????? ??????????? ?? ??????????? ?
  • if ((state (Unstarted Stopped)) 0)
  • // implies Running
  • ?????? ?? ??????? ????? ? ? ????????? Unstarted
  • ?????? ???? ?????? ?? ?? ?????? ?????? ? Start

54
??????????? ? ThreadState
  • Aborted(256)
  • ??????? ? ?????? Abort()
  • ??????? ? ? ????????? Stopped, ???????????? ?
    Aborted
  • AbortRequested(128)
  • Background(4)
  • Running(0)
  • Stopped(16)
  • StopRequested(1)
  • Suspended(64)
  • SuspendRequested(2)
  • Unstarted(8)
  • WaitSleepJoin(32)

55
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • ??????? Abort() ? ??? ???????,
  • ?? ?? ? ???????? ThreadAbortException, ????? ??
    ?? ????? ?? ? ????????
  • Background(4)
  • Running(0)
  • Stopped(16)
  • StopRequested(1)
  • Suspended(64)
  • SuspendRequested(2)
  • Unstarted(8)
  • WaitSleepJoin(32)

56
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • Background(4)
  • ??????? ? ??? ????? ?????
  • ??????? ?? ??? ?????????? Thread.IsBackground
  • Running(0)
  • Stopped(16)
  • StopRequested(1)
  • Suspended(64)
  • SuspendRequested(2)
  • Unstarted(8)
  • WaitSleepJoin(32)

57
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • Background(4)
  • Running(0)
  • ??????? ? ?????????? ? ?? ? ?????????
  • ???? ?????? ?????????? ThreadAbortedException
  • Stopped(16)
  • StopRequested(1)
  • Suspended(64)
  • SuspendRequested(2)
  • Unstarted(8)
  • WaitSleepJoin(32)

58
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • Background(4)
  • Running(0)
  • Stopped(16)
  • ??????? ? ?????????? ?? ?????? ?? Abort() ???
  • ? ?????????? ???????? ?? ??????????
  • StopRequested(1)
  • Suspended(64)
  • SuspendRequested(2)
  • Unstarted(8)
  • WaitSleepJoin(32)

59
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • Background(4)
  • Running(0)
  • Stopped(16)
  • StopRequested(1)
  • ???????? ? ???? ?? ??????? ?? ???? ??????
  • ???? ?? ???????? ??????
  • Suspended(64)
  • SuspendRequested(2)
  • Unstarted(8)
  • WaitSleepJoin(32)

60
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • Background(4)
  • Running(0)
  • Stopped(16)
  • StopRequested(1)
  • Suspended(64)
  • ??????? ? ???? ?????????????
  • ?????????? ????? ???? ? ??????? Suspend(), ???? 1
    Resume ? ??????????
  • SuspendRequested(2)
  • Unstarted(8)
  • WaitSleepJoin(32)

61
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • Background(4)
  • Running(0)
  • Stopped(16)
  • StopRequested(1)
  • Suspended(64)
  • SuspendRequested(2)
  • ??????? ? ?????? Suspend()
  • ??????? ?? ?? ?????? ?? ???????? ?????????, ?? ??
    ?? ???????????
  • Unstarted(8)
  • WaitSleepJoin(32)

62
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • Background(4)
  • Running(0)
  • Stopped(16)
  • StopRequested(1)
  • Suspended(64)
  • SuspendRequested(2)
  • Unstarted(8)
  • ?? ? ??????????, ???????? ?? ??? Start()
  • ???? ???? ?? ???????? ??????? ?????? ?? ???? ???
    ?? ? ? ???? ?????????
  • WaitSleepJoin(32)

63
??????????? ? ThreadState
  • Aborted(256)
  • AbortRequested(128)
  • Background(4)
  • Running(0)
  • Stopped(16)
  • StopRequested(1)
  • Suspended(64)
  • SuspendRequested(2)
  • Unstarted(8)
  • WaitSleepJoin(32)
  • ??????? ? ?????????
  • ????? ? ???? ?? ???????? Thread.Wait(),
    Thread.Sleep(), Thread.Join()

64
???????????? ?? ?????
  • ?? ?? ?? ???? ???? ?????, ?? ???????? ???????
    Thread.Abort()
  • Thread.Abort() ?????? ??????????? ??????????
    ThreadAbortedException
  • ?????? ThreadStateException, ??? ??????? ???? ?
    ??????????
  • ?????????? ?? catch ? finally ?? ????????????
    ?????
  • Thread.ResetAbort() ?? ????????? ?? CLR ?? ??????
    ???????????? ??????
  • Unmanaged code ?????????

65
InterruptAbort
//??? ?????????? public class BackgroundThread
public void doBackgroundJob() try
// ??????? ???? ????? ?? ?????, ? //
???????? ?????????? catch(
ThreadInterruptedException )
MessageBox.Show("ThreadInterruptedEx")
catch( ThreadAbortException )
MessageBox.Show("ThreadAbortedEx")
finally MessageBox.Show("finally block")
MessageBox.Show(After Finally")
(???????? ??????????)
66
InterruptAbort
public class StatusThread private StatusBar
sbStatus private Thread bThread public
StatusThread( StatusBar sbStatus1, Thread
bThread1 ) this.sbStatus sbStatus1
this.bThread bThread1 public void
doBackgroundTask() while( true )
sbStatus.Text bThread.ThreadState.T
oString() Thread.Sleep(10)
(???????? ??????????)
67
InterruptAbort
public InterruptAbort() background new
BackgroundThread() bThread new Thread( new
ThreadStart(background.doBackground) )
bThread.IsBackground true statusThread new
StatusThread() bStatus new Thread( new
ThreadStart(statusThread.doBackground) )
bThread.Start() bStatus.Start() private
void btnControl_Click(...) if( running )
bThread.Suspend() else bThread.Resume()
running !running private void
btnAbort_Click(...) bThread.Abort() private
void btnInterrupt_Click(...) bThread.Interrupt()

68
???????????? 6
  • Priority-State-Suspend-Resume-Interrupt-Abort-Exam
    ple

69
Thread Local Storage
  • ?????????, ? ????? ??????? ????? ?? ??????????
    ?????
  • ????? ?? ???? ?? ??????? ??????? ?? ????? ?????
  • Thread.AllocateNamedDataSlot
  • ???????? ?????????
  • ???????? ???????? ?????
  • Thread.AllocateDataSlot
  • ???? ???, ???? reference
  • ???????? ??? ??????? ??? ?? ???? ? ???? ?????
    ????????? ??? ????

70
Thread Local Storage
class Threads public void createDataThread()
LocalDataStoreSlot slot
Thread.AllocateNamedDataSlot(mySlot)
//????????? ????? ????? ? NamedSlot
Thread.SetData(slot ,IMPORTANT DATA) //
suspend-???? Thread.CurrentThread.Suspend()
// ????????? ?????? ??????? object myData
Thread.GetData(mySlot) public void
readDataThread() LocalDataStoreSlot slot
Thread.GetNamedDataSlot(mySlot) //
???????? ?? ?? ???????? ????????????
Thread.SetData(slot, BAD DATA")
(???????? ??????????)
71
Thread Local Storage
class TLS static void Main(string args)
Threads threads new Threads() Thread
createData new Thread( new
ThreadStart(threads.createDataThread) )
createData.Start() Thread readData new
Thread( new ThreadStart(threads.readDataThre
ad) ) readData.Start()
readData.Join() createData.Resume()
72
???????????? 7
  • ThreadLocalStorage

73
Thread-Relative Static Fields
  • ???????? ?????????? ? ???????? ThreadStaticAttribu
    te
  • ??????????????? ?? ???? ?????????? ?? ???????????
    ???? ? ??????? ????? ?? ?????? ????????, ???????
    ???? ????
  • ??? ?????? ???????? ?????? ?? ???????????? ? null
    (0 ?????????)
  • ????????? ?? ?????????? ?? ???????????? ??
    ???????????? ?????? ??????

74
Thread-Relative Static Fields
class ThreadStatic static void Main(string
args) for( int i0 ilt10 i ) new
Thread(new ThreadStart(new
MyThread().doTask)).Start() class
MyThread ThreadStatic public static int
abc public MyThread() abc42
public void doTask() abc
Console.WriteLine("abc"abc)
75
???????????? 8
  • ThreadStatic

76
?????????? ?? ???????
  • ?????? ????????????? ?? ?????????????? ???????
    ???????
  • ????? ???????? ? ?? ?? ??????????? ? ????????????
    ?? ?????
  • ???????? ? ?????????? ?? ???????? ?????? ???????
    ?? ???????????? ?? ????? ????????? ??
    ???????????? ??
  • ?????????? ?? ?? ?????????? ThreadPool ?????? ?
    ????????
  • ????????? ?? ????? ????? ? ??????

77
????????? ????? - ????????
  • Race condition
  • ??? ????? ???????????? ????????? ???? ? ????
    ?????
  • ???????????? ?????????
  • ???????? ??? ????? ???????????? ?????????? i ??
    ???? ?????????? i
  • Deadlock
  • ?????????, ? ????? ??? ????? ?? ????? ??????? ??
    ????????????? ?? ??????? ????? ???????
  • A ????? ??????? X, B ????? ??????? Y ? ???? ?? X,
    A ??????? ?? ???? ?? Y

78
Race Condition
using System using System.Threading namespace
Demo2DataCorruption class UserAccount
public int cash public void Withdraw100()
int temp cash
Console.WriteLine("withdrawing 100...")
Thread.Sleep(100) temp-100
cashtemp (???????? ??????????)
79
Race Condition
class Bank static void Main(string
args) UserAccount ua new
UserAccount() ua.cash 500 Thread
user1 new Thread( new
ThreadStart(ua.Withdraw100) ) Thread user2
new Thread( new ThreadStart(ua.Withdraw1
00) ) user1.Start() user2.Start()
user1.Join() user1.Join()
Console.WriteLine("result0", ua.cash)

80
???????????? 9
  • ?????????? ?? ?????

81
?????????????
  • ??????????????
  • ?????
  • ?????????????
  • ???-??????? ???????
  • ????????? ?? ?????????????
  • Synchronized Contexts
  • Synchronized code regions
  • MethodImplAttribute
  • Unmanaged Synchronization - WaitHandle
  • ????????? ???????
  • ?????????? ???????????????? ????????
  • ThreadPool
  • ?????????? ??????????

82
???-??????? ???????
  • ???-????? ????????? ??? ????????? ????? ?
    ???????????? ????? ? ??????? ?? ????????? ?????
  • ?????????? ??
  • ?? ?? ?????????? ??????? ? ????????? ? ?????????
    ?? ??????
  • ???? ?????? ?? ?? ?? ???????? ????? ?????
  • ??? ??? ??? ???? ? ??????????, .NET Framework,
    ?????????? ????????? ?? ????????????? ?? ???????
    ?? ?????..

83
????????? ?? ?????????????
  • ?????????????? ????????? (Synchronized Contexts)
  • ?????????????? ?????? ??? (Synchronized code
    regions)
  • MethodImplAttribute (MethodImplOptions.Synchroniz
    ed) ??? ??????????????? ?? ?????
  • Unmanaged ?????????????
  • .NET Framework ?????????? ??????? ?? ????, ?????
    ????????? (custom) ????????????? ? ?????? ?? OS

84
Synchronized Contexts
  • ???????? ?? SynchronizedAttribute ?? ??????
    ??????????? ContextBoundObject
  • ??????, ????? ???????? ? ???? ????????
  • ?????? ????? ? ???? ???????? ????? ?? ?????????
    ?????? ?? ????????? ?? ?????? ??????, ??
    ??????????????
  • ?????????? ??????? ?? ?? ??????????
  • ?? ???????? ???????????????? ?? ??????????
    ???????/???????? ?? ??? ???????????? ???
    ????

85
Synchronized Contexts
using System using System.Threading using
System.Runtime.Remoting.Contexts Synchronization
Attribute class CBO ContextBoundObject
public void doSomeTask1()
Console.WriteLine("job1 started") for( int
i0 ilt1100000000 i ) Math.Sqrt(i)
Console.WriteLine("job1 done\n") public
void doSomeTask2() Console.WriteLine("job2
started") for( int i0 ilt1900000000 i )
Math.Sqrt(i) Console.WriteLine("job2
done\n") (????????
??????????)
86
Synchronized Contexts
class Starter static void Main() CBO
syncClass new CBO() Console.WriteLine("Main
thread Starts 6 Threads - 3 doing job1, 3
doing job2") for( int i0 ilt6 i )
Thread t if( i20 ) t new
Thread( new ThreadStart(syncClass.doSome
Task1)) else t new Thread( new
ThreadStart(syncClass.doSomeTask2))
t.Start() Console.WriteLine("Main
Thread exits..")
87
???????????? 10
  • ContextBoundObject

88
Synchronized code regions
lock( x ) //..code..
Monitor.Enter( x ) try /..code../ finally
Monitor.Exit( x )
  • managed
  • ???????? ? ???????? ??????? ?? ????
  • ???????????? ?? ???? ??????, ????? ? ??????????
    (?????? ?? critical section)
  • Monitor.TryEnter()

89
Synchronized code regions
using System using System.Threading public
class EnterExit private int result
0 public void CriticalSection()
Monitor.Enter(this) try
Console.WriteLine(...) for(int i 1 i lt
5 i) Console.WriteLine(...
) result Thread.Sleep(1000)
Console.WriteLine(...)
finally Monitor.Exit(this)
(???????? ??????????)
90
Synchronized code regions
public static void Main(String args)
EnterExit e new EnterExit( ) Thread
ct1 new Thread(new ThreadStart(e.CriticalS
ection)) ct1.Start( ) Thread
ct2 new Thread(new ThreadStart(e.CriticalS
ection)) ct2.Start( )
//???????? Entered Thread 1 Result 0
ThreadID 1 Result 1 ThreadID 1 Result 2
ThreadID 1 Exiting Thread 1 Entered Thread
2 Result 3 ThreadID 2 Result 4 ThreadID
2 Result 5 ThreadID 2 Exiting Thread 2
91
???????????? 11
  • EnterExit

92
??? ??????? ??????? ??????
  • Monitor.Wait( object )
  • ??????????? monitor-? ?? ????? ? ??????? ???????
    ?????? ?? ?? ??????
  • ?? ?? ??????? ?? waiting queue
  • ???? Pulse(object), PulseAll(object)
  • Timeout overload
  • Monitor.Pulse( object )
  • ??????? ????????? ? ready queue(??? ????? ??
    ????? ???????? ?? ?????)
  • ???? ???????? ?????????? ?? ????????
  • Monitor.PulseAll( object )

93
Interclass Synchronization
public class LockMe public class WaitPulse1
private int result 0 private LockMe lM
public WaitPulse1(LockMe l) this.lM l
public void CriticalSection()
Monitor.Enter(this.lM) Console.WriteLine("Wai
tPulse1Entered") for(int i 1 i lt 5
i) Monitor.Wait(this.lM)
Console.WriteLine("WaitPulse1increase")
result Monitor.Pulse(this.lM)
Console.WriteLine("WaitPulse1Exiting)
Monitor.Exit(this.lM) public class
WaitPulse2 .../the same class/ (????????
??????????)
94
Interclass Synchronization
public class ClassForMain public static void
Main(String args) //??????, ?? ????? ??
???????????? LockMe l new LockMe()
//????????? ????????? ?? WaitPulse1 ?
//WaitPulse2 ???? ?? ???????? LockMe
WaitPulse1 e1 new WaitPulse1(l) WaitPulse2
e2 new WaitPulse2(l) Thread t1 new
Thread(new ThreadStart(e1.CriticalSection))
t1.Start( ) Thread t2 new Thread(new
ThreadStart(e2.CriticalSection))
t2.Start( )
95
???????????? 12
  • Interclass Synchronization

96
MethodImplAttribute (MethodImplOptions.Synchroniz
ed)
  • ??????? ?? lock ?? ??? ?????
  • ???? ?? ???????????? static ???????

MethodImpl(MethodImplOptions.Synchronized)
public void doSomeTask1() Console.WriteLine("
job1 started") for( int i0 ilt1100000000 i
) Math.Sqrt(i) Console.WriteLine("job1
done\n")
97
Unmanaged Synchronization
  • WaitHandle
  • Unmanaged ????????? ?? ?????????????
  • ???????? ?????? ?? ????????????? ??????? ??
    ????????? ?? ???????
  • ?????????? ??????????? ????? ???? ?? CLR
    WaitAll(), WaitAny()
  • ?? ?? ?????????? ?? ? managed
  • - Mutex
  • - AutoResetEvent
  • - ManualResetEvent

98
Thread.WaitHandle
  • ???????? ?????? ?? ????????? ?? ????????? ??
    ???????
  • ????????? signaled, nonsignaled
  • WaitAll (static)
  • ??????? ?????? ?? ?????? ??????? ?? ?????
  • WaitAny (static)
  • ??????? ??????? ???????? ??????
  • WaitOne
  • ??????? ???????? ??????? ?? ????????
  • ??????? ? ??????? ? Monitor

99
Mutex
  • ????????? ?? WaitHandle
  • ???????? ?? ????????????? ?? OS
  • ??????????? Win32 synchronization handles
  • Mutex.WaitOne()
  • Mutex.ReleaseMutex()
  • ?????? ?? ?? ??????, ????? ???? ???? ????
    Mutex.WaitOne()
  • WaitHandle.WaitAll() ?? ?? ?? ??????? ??? ???
    ??????? handle-a

100
Mutex
class MutexDemo Mutex m public MutexDemo(
Mutex m ) this.m m public void
performSomeTask() int ten 10
m.WaitOne() Thread.Sleep(200)
Console.WriteLine("start performing job")
for( int i0 ilt ten i)
Thread.Sleep(100) Console.Write("")
Console.WriteLine("\nstop. job done\n")
m.ReleaseMutex()
101
???????????? 13
  • MutexExample

102
AutoResetEvent/ManualResetEvent
  • ?????????? ?? WaitHandle
  • ????????? ?? ?????????????
  • ??????????? Win32 synchronization handles
  • ????????????? ??? Set()
  • AutoResetEvent ??????????? ???? ??????
    ???????????(handle), ?????? ??????????? ?
    nonsignaled state
  • ManualResetEvent() ??????????? ?????? ??????
    ???????????? ? ?????? ? signaled state, ??????
    ????? ?? ?? ???????

103
AutoResetEvent/ManualResetEvent
using System using System.Threading class
OneWhoWaits WaitHandle handle int
waitTime public OneWhoWaits( WaitHandle
handle, int waitTime ) this.handle
handle this.waitTime waitTime 3000
public void performSomeTask()
Thread.Sleep(waitTime) Console.WriteLine("Thr
ead Thread.CurrentThread.GetHashCode()
" is waiting") handle.WaitOne()
(???????? ??????????)
104
AutoResetEvent/ManualResetEvent
class MainClass static void Main(string
args) ManualResetEvent evnt new
ManualResetEvent(false) int ten 10
for( int i0 iltten i ) OneWhoWaits
oww new OneWhoWaits(evnt, i1)
Thread thread new Thread( new
ThreadStart(oww.performSomeTask) )
thread.Start() for(int i0 iltten i
) Console.ReadLine() evnt.Set()

105
???????????? 14
  • ResetEventExample

106
????????? ???????
  • System.Threading.Interlocked
  • ???????? ????????
  • System.Threading.ThreadPool
  • ???? ?????? ?? ?????
  • ???????????????? ?? ???????
  • ???????????? ?? ?? ????? ?? ?????? ?? ???????
  • System.Threading.ReaderWriterLock
  • ?????????? ???????????????? ????????
    Reader/Writer Problem
  • Synchronized Wrappers

107
System.Threading.Interlocked
  • ???????? ???????? ????????
  • ???? ????? ????????????? ?? ??????????? ?????
  • ? ???????? ??-?????? ?????? ??????????????? ????
    ?? ?? ??????? ? ???? ??????
  • ???????? ?? ??????? ??????????

108
System.Threading.Interlocked
  • Increment/Decrement
  • ???????? ???????? ?? ??????? ?? i/i--
  • Exchange
  • ??????? ?????????? ?? ??????? ????????? ? ??????,
    ????? ?????????
  • CompareExchange
  • ??? ??? ??????????
  • ????????? ???? ??????? ? ??????? ?? ?????, ???
    ??, ??????? ?????? ? ??????
  • ???????? ?? ??? ?????? ? ???????? ??????????

109
System.Threading.Interlocked
class CompareExchangeTest private int
US_Dollars1000 public void
add_US_Dollars(int more_Dollars) int
new_Dollars int initial_Dollars do
initial_Dollars US_Dollars
new_Dollars US_Dollars more_Dollars
Thread.Sleep(1000) while(
initial_Dollars ! Interlocked.CompareExchan
ge(ref US_Dollars, new_Dollars,
initial_Dollars) )
110
ThreadPooling
  • ????? ?? ??????? ????????? ?????? ???? ?? ??????
    ?? ?????? (ThreadState.WaitSleepJoin) ??? ???????
    ??????? ???????
  • ????? ?? ???????? ???? ?? ????? ???????, ?? ??
    ???????? ?????????? ?? ??????? ??? ?? ?? ????????
    ???????
  • ???????????? ?? ????? ????????? ????? ? ???????,
    ??????????? ? ??????? ???????

111
ThreadPooling
  • TP ? ????????? ?? ?????????? ?? overload-? ???
    ????????? ? ??????????? ?? ?????
  • TP ? ??????? ?? ????????? ?? ????? ????? ?
    ???????? ?? ???????????? ??????????
  • ????? ????? ????? ? ?.???. Thread Pool
  • ??????? ?? ??????? ?????(worker threads)
  • ??? ???? ??????, ?? ???????? worker thread, ????
    ???? ?? ????? ??????? ? ???????
  • TP ?? ????? ?? ?????? ? ??????????????? ????????
    ?? ???????
  • ???????????? ????? ?? ??????????? ?? ???? ?????

112
System.Threading.ThreadPool
  • .NET Framework ???????????? ????????? Thread
    Pooling ? ????? ThreadPool
  • ?????????? ???-?????? ? ???????? ?????? ?? ?????
  • ???? ?? ??? ???? ???? ThreadPool ????? ?? ????
    ??????, ??????? ?? ??? ???????
  • ????????? ?? QueueUserWorkItem
  • ???????????? ??????
  • ???????????? ?? wait ???????? ? callback ?????
  • ?? ???? ?? ?? ???? ?????? ??? ? ? ThreadPool
  • ?? ???????????? ? ThreadPool ??? ????? ?? 25
    ????? ?? ????????. ???? ?? ?? ???????
  • .NET Framework ???????? Thread Pooling ??
    ?????????? ??????????, ??????, ???????, wait
    ????????, ?????????? I/O, ...

113
System.Threading.ThreadPool
  • ThreadPooling ????????? ?? OS ?? ??????????
    ???????????? ?? ?????, ??? ???? ???? ?? ?
    ????????
  • ???? ?????? ?? ???? ?? ???????, ?? ? ?? ??????
    ???????
  • ??????, ? ????? ? ???????????????? ????????????
    ?? ThreadPool
  • ????? ?? ??????????
  • ?????? ???????? ????? ?????, ???? ?? ???????
    ????? ????????
  • ????? ?? STA (single threaded apartment), ??????
    ????? ? ThreadPool ?? ? MTA (multi threaded
    apartment)
  • ?????????? ?? ???????????? ?????
  • ?????????????

114
ThreadPool.QueueUserWorkItem
using System using System.Threading class
ThreadPoolDemo public void LongTask1(object
param) Console.WriteLine(text sent 0,
param) static void Main() string
phrase Hello World ThreadPoolDemo tpd
new ThreadPoolDemo()
ThreadPool.QueueUserWorkItem(new
WaitCallback(tpd.LongTask1), phrase)
115
ThreadPool.QueueUserWorkItem
using System using System.Threading class
ThreadPoolDemo public void LongTask1(object
param) Console.WriteLine(text sent 0,
param) static void Main() string
phrase Hello World ThreadPoolDemo tpd
new ThreadPoolDemo()
ThreadPool.QueueUserWorkItem(new
WaitCallback(tpd.LongTask1), phrase)
116
ThreadPool.QueueUserWorkItem
using System using System.Threading class
ThreadPoolDemo public void LongTask1(object
param) Console.WriteLine(text sent 0,
param) static void Main() string
phrase Hello World ThreadPoolDemo tpd
new ThreadPoolDemo()
ThreadPool.QueueUserWorkItem(new
WaitCallback(tpd.LongTask1), phrase)
117
ThreadPool.QueueUserWorkItem
using System using System.Threading class
ThreadPoolDemo public void LongTask1(object
param) Console.WriteLine(text sent 0,
param) static void Main() string
phrase Hello World ThreadPoolDemo tpd
new ThreadPoolDemo()
ThreadPool.QueueUserWorkItem(new
WaitCallback(tpd.LongTask1), phrase)
118
ThreadPool.QueueUserWorkItem
using System using System.Threading class
ThreadPoolDemo public void LongTask1(object
param) Console.WriteLine(text sent 0,
param) static void Main() string
phrase Hello World ThreadPoolDemo tpd
new ThreadPoolDemo()
ThreadPool.QueueUserWorkItem(new
WaitCallback(tpd.LongTask1), phrase)
119
ThreadPool.QueueUserWorkItem
using System using System.Threading class
ThreadPoolDemo public void LongTask1(object
param) Console.WriteLine(text sent 0,
param) static void Main() string
phrase Hello World ThreadPoolDemo tpd
new ThreadPoolDemo()
ThreadPool.QueueUserWorkItem(new
WaitCallback(tpd.LongTask1), phrase)
120
ThreadPool.QueueUserWorkItem
using System using System.Threading class
ThreadPoolDemo public void LongTask1(object
param) Console.WriteLine(text sent 0,
param) static void Main() string
phrase Hello World ThreadPoolDemo tpd
new ThreadPoolDemo()
ThreadPool.QueueUserWorkItem(new
WaitCallback(tpd.LongTask1), phrase)
121
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
122
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
123
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
124
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
125
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
126
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
127
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
128
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
129
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
130
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
131
ThreadPool.RegisterWaitForSingleObject
using ... public class Example public static
void Main(string args) AutoResetEvent ev
new AutoResetEvent(false) object param
"some param" RegisteredWaitHandle r
ThreadPool.RegisterWaitForSingleObject(
ev, new WaitOrTimerCallback(WaitProc),
param, 1000, false ) Console.ReadLine()
Console.WriteLine("signaling.") ev.Set()
Console.ReadLine() Console.WriteLine("unregi
ster wait") r.Unregister(ev)
Console.ReadLine() public static void
WaitProc(object param, bool timedOut)
string cause "SIGNALED" if (timedOut)
cause "TIMED OUT" Console.WriteLine("WaitP
roc executescause 0", cause)
132
???????????? 15
  • RegisterWaitForSingleObject

133
Classical synchronization problems
  • The Producer-Consumer Problem
  • ???? ?????????? ? ???? ???????????? ???????? ???
    ?????
  • ??????????
  • ??? ??????? ? ??????, ???????????? ?? ?????? ??
    ???? ?? ?? ????????
  • ??? ??????? ? ?????, ?????????????? ?? ???? ??
    ?????? ???????
  • ?????????????? ? ???????????? ?? ????? ??
    ????????? ?????? ????????????
  • ?????? ???????? ?????
  • ?????? ?? ??? bounded buffer problem
  • ???? ?????????????? ??????? ? .NET

134
Classical synchronization problems
  • The Readers-Writers Problem
  • ???? ??? ?????? ?????? ? ???? ??? ?????? ??????
    ????????? ??? ?????
  • ?????? ?????? ????? ? ???? ?? OS
  • The Bernstein conditions
  • ??????? ????? ????? ?? ????????? ????? ?????
  • ?????? ????? ???????? ??????, ???? ????? ????
    ???? ????? ?? ???? ?? ???????? ??????
  • ?????? ?? ???? ???? ?????/????? ?? ?? ????
    ????????? ????? ?? ?????????? ??????
  • ? .NET ReaderWriterLock

135
System.Threading.ReaderWriterLock
  • ???????????? ???-?????????? ????????????????
    ??????? Reader/Writer Problem
  • IsReaderLockHeld
  • IsWriterLockHeld
  • AcquireReaderLock
  • AcquireWriterLock
  • ReleaseReaderLock
  • ReleaseWriterLock

136
System.Threading.ReaderWriterLock
using System using System.Threading class
Resource ReaderWriterLock rwl new
ReaderWriterLock() public void Read(Int32
threadNum) rwl.AcquireReaderLock(Timeout.Inf
inite) try //many can read
processing, writers blocked finally
rwl.ReleaseReaderLock() public
void Write(Int32 threadNum)
rwl.AcquireWriterLock(Timeout.Infinite) try
// one can write, readers blocked
finally rwl.ReleaseWriterLock()

137
Classical synchronization problems
  • The Dining Philosophers Problem
  • ??????? ???????? ?????????? ?????? ???? ?
    ???????? ??? ??????
  • ?????? ?? ??????, ?? ???????? ?? ????? ?????? ??
    ????? ????????
  • ???????
  • ?? ?????? ????? ??????? ?? ???? ?? ????
  • ?? ?????? ?? ?? ????????? deadlock ?????? ?????
    ??????? ??? ?? ???? ?????? ? ???? ?? ???????

138
CustomReaderWriter
class CustomReaderWriter private int readers
0 private bool isWriting false public
void Read() lock( this )
while( isWriting ) Monitor.Wait( this )
readers //...READING TAKES PLACE
HERE... lock( this )
readers-- if( readers 0 )
Monitor.Pulse(this)
(???????? ??????????)
139
CustomReaderWriter
class CustomReaderWriter private int readers
0 private bool isWriting false public
void Read() lock( this )
while( isWriting ) Monitor.Wait( this )
readers //...READING TAKES PLACE
HERE... lock( this )
readers-- if( readers 0 )
Monitor.Pulse(this)
(???????? ??????????)
140
CustomReaderWriter
class CustomReaderWriter private int readers
0 private bool isWriting false public
void Read() lock( this )
while( isWriting ) Monitor.Wait( this )
readers //...READING TAKES PLACE
HERE... lock( this )
readers-- if( readers 0 )
Monitor.Pulse(this)
(???????? ??????????)
141
CustomReaderWriter
class CustomReaderWriter private int readers
0 private bool isWriting false public
Write a Comment
User Comments (0)
About PowerShow.com