我正在尝试读取文件示例输出,应该是 字串1 字串2 字符串3
我以3个线程为例,我想读取1个文件,这是我启动线程的代码
For i = 0 To 3 - 1
Dim HTTPFlood As New Threading.Thread(AddressOf TEST)
HTTPFlood.Start()
LIST.Add(HTTPFlood)
Next
这就是即时消息用来显示输出的即时消息,我试图先将文件分成3个豌豆,然后试图使每个线程收集另一个文件以使其正确显示,但是它给了我类似的东西
字符串1 字串1 字符串2
public sub TEST
thread_connect += 1
thread_file = "C:\Users\msfde\Desktop\b_" + thread_connect + ".txt"
For Each element As String In File.ReadAllLines(thread_file)
console.writeline(element)
next
任何人都可以帮助我阅读文件吗
字符串1 字符串2 字符串3
代替
字符串1 字符串1 字符串1
我希望有人可以帮助我坐在这里48小时,我已经尽一切努力使每个线程读到另一行,但是太难了
答案 0 :(得分:0)
首先,由于硬件的限制,使用线程读取文件不会加快速度。其次,我不知道thread_connect是如何创建的,但是它看起来不是线程安全的。这意味着,可能有2个线程增加了thread_connect,创建thread_file,然后两者都将使用相同的thread_file作为变量,并且内部具有相同的值。您需要使其线程安全。
Public threadConnect As Integer = 0
Public lockObject As New Object
public sub TEST
Dim filename As String ' Local variable, threads won't share the same variable
SyncLock lockObject ' Lock the thread to make sure they don't access the shared threadConnect variable at the same time
threadConnect += 1
filename = "C:\Users\msfde\Desktop\b_" + threadConnect + ".txt"
End SyncLock
For Each element As String In File.ReadAllLines(filename)
console.writeline(element)
next