线程安全、应用程序设计
我有一个控制台应用程序,它循环遍历队列并处理数据表中的每条记录。每个过程都是漫长且耗时的(连接到远程网站、下载文件、读取文件并将其添加到数据库)。我试图为了速度而实现线程,但遇到了跨线程问题。 (该作业需要 8 小时左右才能运行,并且直到运行几个小时后才会开始发生跨线程。)
我正在尝试找出如何使我的代码线程安全。我不更新或依赖任何非局部变量,也不需要返回任何内容。我尝试过锁定,它有效,但违背了多线程的目的。我还应该寻找哪些其他东西?
Module Module1
Dim iThread As Integer
Dim manualEvents(4) As ManualResetEvent
Private lockObject As New Object()
Sub Main()
' some prep work
For i = 0 To 4
manualEvents(i) = New ManualResetEvent(False)
ThreadPool.QueueUserWorkItem(AddressOf DoOne)
Next
For Each handle As WaitHandle In manualEvents
handle.WaitOne()
Next
' some cleaning
End Sub
Private Sub DoOne()
If QueryQueue Then
DoOne()
Else
manualEvents(iThread).Set()
iThread = iThread + 1
End If
End Sub
Public Function QueryQueue() As Boolean
Dim dt As DataTable = GetData()
If dt.Rows.Count > 0 Then
With dt
Dim Variable1 As String = .Rows(0).Item("Variable1")
Dim Variable2 As String = .Rows(0).Item("Variable2")
Dim Variable3 As Integer = .Rows(0).Item("Variable3")
ProcessRecord(Variable1, Variable2, Variable3)
End With
Return True
Else
Return False
End If
End Function
Public Sub ProcessRecord(ByVal Variable1 As String, ByVal Variable2 As String, ByVal Variable3 As Integer)
AnotherMethod(Variable1, Variable2, Variable3)
End Sub
Public Sub AnotherMethod(ByVal Variable1 As String, ByVal Variable2 As String, ByVal Variable3 As Integer)
AnotherMethod2(Variable1, Variable2, Variable3)
End Sub
Public Sub AnotherMethod2(ByVal Variable1 As String, ByVal Variable2 As String, ByVal Variable3 As Integer)
AnotherMethod3(Variable1, Variable2, Variable3)
End Sub
' ... etc.
End Module
I have a Console app that loops through a queue and processes each record in a datatable. Each process is long and time-consuming (connecting to a remote website, downloading files, reading them and adding them to the database). I'm trying to implement threading for speed's sake but am having cross-threading issues. (The job takes 8 hours or so to run and the cross-threading doesn't start happening until a couple hours into the run.)
I'm trying to figure out how to make my code thread safe. I don't update or rely on any non-local variables, and I don't need to return anything. I've tried locking which works but defeats the purpose of the multiple threads. What other things should I be looking for?
Module Module1
Dim iThread As Integer
Dim manualEvents(4) As ManualResetEvent
Private lockObject As New Object()
Sub Main()
' some prep work
For i = 0 To 4
manualEvents(i) = New ManualResetEvent(False)
ThreadPool.QueueUserWorkItem(AddressOf DoOne)
Next
For Each handle As WaitHandle In manualEvents
handle.WaitOne()
Next
' some cleaning
End Sub
Private Sub DoOne()
If QueryQueue Then
DoOne()
Else
manualEvents(iThread).Set()
iThread = iThread + 1
End If
End Sub
Public Function QueryQueue() As Boolean
Dim dt As DataTable = GetData()
If dt.Rows.Count > 0 Then
With dt
Dim Variable1 As String = .Rows(0).Item("Variable1")
Dim Variable2 As String = .Rows(0).Item("Variable2")
Dim Variable3 As Integer = .Rows(0).Item("Variable3")
ProcessRecord(Variable1, Variable2, Variable3)
End With
Return True
Else
Return False
End If
End Function
Public Sub ProcessRecord(ByVal Variable1 As String, ByVal Variable2 As String, ByVal Variable3 As Integer)
AnotherMethod(Variable1, Variable2, Variable3)
End Sub
Public Sub AnotherMethod(ByVal Variable1 As String, ByVal Variable2 As String, ByVal Variable3 As Integer)
AnotherMethod2(Variable1, Variable2, Variable3)
End Sub
Public Sub AnotherMethod2(ByVal Variable1 As String, ByVal Variable2 As String, ByVal Variable3 As Integer)
AnotherMethod3(Variable1, Variable2, Variable3)
End Sub
' ... etc.
End Module
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
iThread = iThread + 1
不是线程安全的。您应该改为调用 Interlocked.Increment 。
如果您使用 .Net 4.0,则应该使用新的
Task
API 而不是 ThreadPool;然后您可以调用Task.WaitAll
。iThread = iThread + 1
is not thread safe.You should call
Interlocked.Increment
instead.If you're using .Net 4.0, you should use the new
Task
API instead of the ThreadPool; you can then callTask.WaitAll
.