Thread。Sleep(1500)
elements。Add(30)
End Sub
Sub Main()
elements。Add(10)
elements。Add(20)
Dim thread1 As New Thread(AddressOf Task1)
Dim thread2 As New Thread(AddressOf Task2)
thread1。Start()
thread2。Start()
End Sub
End Module
The change is the bolded code; which instantiates the type System。Collections。
ReadOnlyCollection; to which we pass the elements list。 The ReadOnlyCollection provides the
base class for a generic read…only collection。 The For Each iterator then iterates a collection
that is read…only; but based on the original collection。 However; running the code will result in
the same exception。 This demonstrates that ReadOnlyCollection does not take a snapshot; but
masks the collection。 The mask disables the addition of items to the collection; but because the
other thread is taking a shortcut and editing the original collection; the read…only collection is
modified as well。
Let’s say that converting the collection into a read…only collection had worked。 It would
not have solved anything。 A read…only collection means that the second thread would generate
an exception because you can’t add elements to a collection that is read…only。 The point is that
when writing multithreaded code that shares variables; you don’t have an easy solution; because
you are trying to solve the problem of how to keep multiple cooks productive in a single kitchen。
We are trying to solve a classic reader/writer problem; where some threads are interested
only in reading the data; and other threads are interested only in modifying the data。 One way
to synchronize the readers and writers is to use an exclusive lock; so that only one thread may
read or write。
…………………………………………………………Page 374……………………………………………………………
352 CH AP T E R 1 3 ■ L E A R N I N G A B OU T M U L T I TH R E A DI N G
Using Exclusive Locks
When using exclusive locks in ; you are saying; “Only one thread may execute this piece
of code。” If two threads want to execute a particular piece of code; one will be granted access;
while the other thread waits until the granted thread has exited the code block。 It is important
to understand that an exclusive lock grants access to code; not data; but that code could access
data。 And because only one thread is accessing the code; it is implied that only one thread can
access the data。
The following is an example of code that uses exclusive locks。
。 。 。
Module ThreadProblem
Dim elements As List(Of Integer) = New List(Of Integer)()
Sub Task1()
Thread。Sleep(1000)
SyncLock elements
Dim item As Integer
For Each item In elements
Console。WriteLine(〃Item (〃 & item & 〃)〃)
Thread。Sleep(1000)
Next
End SyncLock
End Sub
Sub Task2()
Thread。Sleep(1500)
SyncLock elements
elements。Add(30)
End SyncLock
End Sub
Sub Main()
elements。Add(10)
elements。Add(20)
Dim thread1 As New Thread(AddressOf Task1)
Dim thread2 As New Thread(AddressOf Task2)
thread1。Start()
thread2。Start()
End Sub
End Module
The bolded lines use the SyncLock and End SyncLock keywords; which represent a code block of
exclusive access。 The thread is granted access to only a single code block in each instance。
Looking at the code within the block; you can see that the collection is accessed in two locations。
Using the exclusive SyncLock argument where a single thread can access only a single code
block; one thread will write to the collection; and another thread will read from the collection。
…………………………………………………………Page 375……………………………………………………………
C HA P TE R 1 3 ■ L E AR N IN G AB O U T M U L T IT HR E AD IN G 353
The SyncLock statement has a parameter that is a reference to lock against。 In both threads;
the reference is elements。 The mon reference synchronizes access to code。 At any given
point in time; the code contained within the SyncLock block will have only a single thread
executing。 This implements the desired feature; where only one thread is accessing code that
reads or writes to the collection。 The flow of the program is as follows:
1。 Both threads wait。
2。 After a 1 second; thread 1 acquires a lock because no other thread has done so。
3。 Thread 1 executes its code。
4。 Once thread 1 has started executing the synchronized code; no other code can acquire
the lock that is associated with the variable elements。
5。 When thread 2 wakes up after a sleep of 1。5 seconds; it will attempt to acquire the lock;
but it can’t because thread 1 is still holding the lock。 So the second thread must wait。
6。 Thread 1 eventually exits the synchronized code block; allowing the second thread to
add an element to the collection。 This time; no exception is thrown。
The reference to lock against does not need to be the reference that is manipulated within
the code block。 The reference is just that: an arbitrary reference。 You could use a different
object instance; and even instantiate an object like this:
Dim _syncRoot As Object = New Object()
。 。 。
SyncLock _syncRoot
。
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 最新章:第230章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章