是否可以将文件循环分成多个线程?
我正在尝试实现我提出的另一个问题的解决方案(http://stackoverflow.com/questions/7166223/create-password-breaker-for-iphone-backup-files)。解决方案非常简单。问题是我正在使用一本大约有 250 000 个单词的字典。对于每个单词,我都会以某种模式添加字母和数字,以获得我通常使用的不同组合。我去掉了我很少使用的组合,但每个单词仍然有大约 24 个组合,因此最终的单词列表将是 600 万左右。
因此,创建列表的过程非常慢。我在想多线程是否可以解决我的问题。我的理论是,我可以说 4 个线程(我是线程新手,不知道这是否可能)。在线程 1 中,我使用字典的前四分之一,在线程 2 中,我使用第二个四分之一,依此类推。每个线程都会循环遍历字典中的单词,并添加不同的组合。当每个线程完成时,它会将结果写入线程特定的文本文件。当所有线程完成其工作后,我会将不同的文件连接成一个大文本文件(其中包含所有 600 万个单词)。从而将处理时间缩短为 4。至少这是我所希望的。 :=)
我正在使用 C# 工作。这可能吗?简而言之:是否可以在 C# 中使用不同线程循环遍历文本文件的不同部分?有什么我应该特别考虑的吗?
我会尝试尝试一下,但非常感谢您提出的任何建议。
I am trying to implement a solution I got to another question I asked (http://stackoverflow.com/questions/7166223/create-password-breaker-for-iphone-backup-files ). The solution is very straight forward. The problem is that I am using a dictionary with about 250 000 words in it. For every word I add letters and numbers in certain patterns, to get the different combinations I normally use. I have taken away the combinations I rarely use, but still have about 24 combinations for every word, hence the final list of words will be like 6 million or so.
The process of creating the list is therefor VERY slow. I was thinking if perhaps multithreading could solve my problem. My theory is that I could have say 4 threads (I am new to threads, and do not know if that is possible). In thread 1 I use the first fourth of the dictionary, in thread 2 the second fourth, and so on. Every thread loops through the words in it's part of the dictionary, and add the different combinations. When each thread is finished it would write the result to thread specific text files. When all threads have done their work, I would concatenate the different files, into one big text file (with all the 6 million words in it). Thereby cutting processing time in 4. At least that is what I am hoping for. :=)
I am working in C#. Is this possible? So in short: Is it possible to loop through different sections of a text file using different threads in C#? Is there anything in particular I should think about?
I will try to experiment with it, but any advice you might have is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是很有可能的,假设你有一种快速的方法来划分列表(我认为打破中间的单词会很糟糕)。
但是,请记住,除非您有空闲的处理能力可供使用,否则线程不会执行任何操作。如果您使用的是单 CPU/单核 PC,您将尽可能快地运行。但是,如果您有多个 CPU(或至少是多个核心),那么这是有机会的。
实施相当简单。如果您当前正在这样做:
那么它只是:
It's quite possible, assuming that you have a fast means of dividing the list (I assume breaking mid-word would be bad).
But, keep in mind, threads do nothing unless you have spare processing power to put to use. If you are on a single-CPU/single-core PC, you're going as fast as you can. But, if you have multiple CPUs (or at least multiple cores), then this has a chance.
Implementing is fairly simple. If you are currently doing this:
then it's just:
由于这是我第一次尝试使用线程,我想我可以分享我实现的解决方案。如果有人对如何改进这一点有任何建议,那就太好了。我对线程的理解是,当线程与相同的方法或变量交互时,使用线程可能是一个大问题。但我认为我确实将线程完全分开了。他们正在使用我创建的类的不同实例。因此,如果有人对这种使用线程的方式有一些好的建议,我很高兴。 :=) 这是我用来渲染线程的代码:
这是保存渲染密码的代码(在 PasswordRendering 类中):
Since this was my first attempt at using threads, I thought I might share the solution I implemented. If anyone has any suggestions on how to improve this for another time it would be great. My understanding of threads is that it can be a big problem using threads when they are interacting with same methods or variables. But I think that I did separate the threads completely. They are using different instances of a class I created. So if anyone has some good advice on this way of using threads, I am happy. :=) Here is the code I used to render the threads:
And here is code that saves the rendered passwords (In PasswordRendering class):