Abhisek
Abhisek

Reputation: 715

multithread handling in java

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

Upvotes: 0

Views: 282

Answers (2)

John Kane
John Kane

Reputation: 4443

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/

Upvotes: 0

Richard H
Richard H

Reputation: 39055

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.

Upvotes: 2

Related Questions