开发者

Multi-Threaded application, making new threads on the fly?

Right now I have a program with a Gui that indexes a url that I specify. I need to index 15 things at a time and I have been just opening 15 windows of the program and individually inputting the urls that i want to index. However these 15 url's change every hour or so... I have a separate program that stores these 15 constantly changing urls in a table in my mysql database. I am able to fetch these urls from my database(I store them in an arraylist) but im not sure how to go about multi-threading my application so that i dont have to do the manual work of inputing the开发者_如何学Go urls into my application.

My question: Can someone give me an example/link me to a tutorial of how I would go about creating a new thread for each url in my arraylist(also this arraylist will change so will i need to make a new thread for changing this arraylist aswell?)

Ive looked at the java site on concurrency and high level concurrency but didn't really understand the examples they gave(I am still a beginning programmer so please bear with me)

Hopefully I explained what im trying to do with enough detail Thanks in advance

EDIT: The urls i index change every couple of seconds which is why i dont think i can go through my method with each url one after another, hence why i believe it needs to be multithreaded?

2nd EDIT(I believe these guys understand what im asking):

@Jon Storm There are two issues going on 1) Getting URL list 2) Accessing said URLs. I would make the URL fetcher single-threaded and then dispatch out to a Thread Pool of fetchers. This dispatcher can also queue pending fetches, etc. – pst

@Jon Storm: could you please update your question to describe what you want to do more explicitely? If In understood correctly, you want to index a list of 15 URLs again and again, because the contents of the pages at these URLs changes every 3 seconds. And you want to update the list of URLs to index every hour, by getting them from a database. Is that right? – JB Nizet


It seems to me that your problem is not with multi-threading, but with inputting something in a GUI from the application fetching the URLs from the database.

Why don't you simply reuse the class (or some of the code, if it's impossible to reuse the classes as is) of your GUI application (i.e. the URL indexing method) inside the application which fetches the URLs from the database.

My guess is that you could very well index these 15 URLs one after the other, in a single thread. I would try doing that before trying to use threads.

The program would look like this:

  1. Fetch the 15 URLs from the database and put them into a List
  2. Iterate through the list and index each URL
  3. Sleep for some time,
  4. Go to 1

EDIT:

Since it seems the URLs must be indexed again and again until the list of URLs changes, I would use this algorithm:

  1. Create a thread pool using Executors.newCachedThreadPool()
  2. Get the URLs from the database
  3. For each URL, create a task which will index the URL again and again, until interrupted (check that Thread.interrupted() returns false at each iteration)
  4. submit each task to the executorService created at step 1, and keep the returned Future inside a list
  5. Sleep/wait until the list of URLs to index changes
  6. Cancel each Future (cancel(true)) of the list of Future instances
  7. Go to step 2.


SwingWorker, shown here, is a good choice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜