java中的多线程处理
我陷入了一个严重的问题。我正在向服务器发送一个请求,其中包含一些 URL 作为其数据。如果我解释一下,就像我有一个文件,其中按顺序包含一些 URL,我必须使用线程读取这些连续数据。现在的问题是有十万个 URL,我必须在特定时间(假设 30 秒)发送服务器中的每个 URL。所以我必须创建将在所需时间服务于任务的线程。但是我必须以这样的方式读取文件,如果第一个线程提供前 100 个 URL,那么第二个线程将提供下一个 100 个 URL,其他线程也以同样的方式。我正在套接字编程中进行,所以有我一次只能使用一个端口。那么如何解决这个问题呢。给我一个好的简单的想法,如果可能的话也给我一个例子。
提前致谢
I have stuck in a serious problem. I am sending a request to server which contains some URL as its data.If I explain it , it is like I have a file which contains some URL in a sequential order I have to read those sequential data by using thread. Now the problem is there are one hundred thousand URL, I have to send each URL in the server in a particular time(say suppose 30 seconds).So I have to create threads which will serve the task in the desired time. But I have to read the file in such a way if first thread serve first 100 URL then 2nd thread will serve the next 100 URL and in the same way the other threads also.And I am doing it in a socket programming,so there is only one port at a time that I can use. So how to solve this problem. Give me a nice and simple idea, and if possible give me an example also.
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很好又简单的想法(如果我正确理解你的问题):你可以使用 LinkedList 作为队列。从文件中读取 1,000 个 url 并将它们放入列表中。生成您的线程,然后从列表中拉出(并删除)接下来的 100 个 url。但 LinkedList 不是线程安全的,因此您必须自己同步访问。
Nice and simple idea (if I understand your question correctly): You can use a LinkedList as a queue. Read in the 1,000 urls from file and put them in the list. Spawn your threads, which then pull (and remove) the next 100 urls from the list. LinkedList is not thread-safe though, so you must synchronize access yourself.
您可以研究的一件事是 fork/join 框架。 java 教程对此的解释是:“它是为可以递归地分解为更小的部分的工作而设计的。目标是使用所有可用的处理能力来使您的应用程序快速运行”。那么你真正需要做的就是弄清楚如何分解你的任务。
http://download.oracle.com/javase/tutorial/essential/concurrency /forkjoin.html
您可以在以下位置找到该 jar:http://g .oswego.edu/dl/concurrency-interest/
One thing that you could look into is the fork/join framework. The way that the java tutorials explains this is: "It is designed for work that can be broken into smaller pieces recursively. The goal is to use all the available processing power to make your application wicked fast". Then all you really need to do is figure out how to break up your tasks.
http://download.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html
you can find the jar for this at: http://g.oswego.edu/dl/concurrency-interest/