多线程问题 [英] multithreading issue

查看:74
本文介绍了多线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个多线程爬虫,其过程只是创建线程并使它们访问要爬网的URL列表.然后,他们访问网址并解析html内容.所有这些似乎都可以正常工作.现在,当我需要写数据库中的表时,就是遇到问题了.我有2个声明的arraylists,其中将包含每个线程解析的内容.第一个arraylist只是rss feed链接,另一个arraylist包含不同的帖子.
然后我对每个循环使用a来迭代一个循环,同时依次递增另一个循环并写入数据库.
我的问题是每次新线程访问列表之一时,内容都会更改,这会影响迭代.我尝试使用嵌套循环,但之前没有用过,并且使用单线程也可以正常工作.我希望这是有道理的.这是我的代码:

 SyncLock dlock
                    对于每个l作为字符串输入链接
                        finallinks.Add(l)
                    下一个
                结束同步锁
               
                    SyncLock dlock
                    对于每个p作为字符串在帖子中
                        finalposts.Add(p)
                    下一个
                    结束同步锁
...
                    Dim i As Integer = 0
SyncLock dlock
     对于每个rsslink作为finallinks中的字符串
                 postlink = finalposts.Item(i)
                 我=我+ 1
                 

finallinks和finalposts是两个arraylist.我没有包括显示线程正常工作的其余代码,但这是发生错误的必要部分,基本上在这里

代码:

解决方案

感谢您的信息!我建议将您的问题发布到MS论坛之一中,


I have written a multithreaded crawler and the process is simply creating threads and having them access a list of urls to crawl. They then access the urls and parse the html content. All this seems to work fine. Now when I need to write to tables in a database is when I experience issues. I have 2 declared arraylists that will contain the content each thread parse. The first arraylist is simply the rss feed links and the other arraylist contains the different posts.
I then use a for each loop to iterate one while sequentially incrementing the other and writing to the database.
My problem is that each time a new thread accesses one of the lists the content is changed and this affects the iteration. I tried using nested loops but it did not work before and this works fine using a single thread.I hope this makes sense. Here is my code:

  SyncLock dlock
                    For Each l As String In links
                        finallinks.Add(l)
                    Next
                End SyncLock
               
                    SyncLock dlock
                    For Each p As String In posts
                        finalposts.Add(p)
                    Next
                    End SyncLock
...
                    Dim i As Integer = 0
SyncLock dlock
     For Each rsslink As String In finallinks
                 postlink = finalposts.Item(i)
                 i = i + 1
                 

finallinks and finalposts are the two arraylists. I did not include the rest of the code which shows the threads working but this is the essential part where my error occurs which is basically here

Code:

解决方案

Hi,

 

Thank you for your post!  I would suggest posting your question in one of the MS Forum,


这篇关于多线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆