Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Threads and java.net.URL.openConnection()

Reply
Thread Tools

Threads and java.net.URL.openConnection()

 
 
Alun Harford
Guest
Posts: n/a
 
      10-31-2005
I have created a class whose purpose is to download a list of java.net.URLs,
and put the content (of each URL) in to an array of Strings, by creating n
threads and having each thread pick up the next URL from the list, and
download the HTTP content (in a thread-safe way).
However, when it gets to the end of the list, a few threads have hung and
won't timeout. I've tried interrupting them, but that doesn't help (since
they're blocked downloading the URL, I think).

I'd like to know how to kill these threads and try again, or else is there a
safer way of downloading HTTP content from a URL?
Any ideas?

Thanks,
Alun Harford

(Note: I'm using Java 1.4.2 and Sun's JVM)

<LONG NASTY HACKY CODE>
public class ThreadedURLDownloader {
private static final int MAX_THREADS = 100;
private java.net.URL[] urls;
private int currentURL=0;
private String[] contents;
private String[] contentTypes;
private Integer[] responseCodes;
private URLDownloaderThread[] threads=new
URLDownloaderThread[MAX_THREADS];;

public ThreadedURLDownloader(java.net.URL[] urls) {
this.urls=urls;
contents=new String[urls.length];
contentTypes=new String[urls.length];
responseCodes=new Integer[urls.length];
}

public void downloadURLs() throws InterruptedException {
for(int i=0;i<MAX_THREADS;i++) {
threads[i]=new URLDownloaderThread();
threads[i].setThreadedURLDownloader(this);
threads[i].start();
}
for(int i=0;i<MAX_THREADS;i++) {
threads[i].join();
}
}

public synchronized int getNextURLNumber() throws FinishedException {
if(currentURL<=urls.length) {
System.out.println(currentURL+1);
return currentURL++;
} else {
throw new FinishedException();
}

}
public java.net.URL getURL(int index) {
return urls[index];
}
public void setContent(String content, int index) {
contents[index]=content;
}
public void setContentType(String contentType, int index) {
contentTypes[index]=contentType;
}
public void setResponseCode(int code, int index) {
responseCodes[index]=new Integer(code);
}
public String[] getContents() {
return contents;
}
}
public class URLDownloaderThread extends Thread {
ThreadedURLDownloader tud;
public URLDownloaderThread() {
super();
}
public void setThreadedURLDownloader(ThreadedURLDownloader tud) {
this.tud=tud;
}
public void run() {
try {
while(true) {
String content;
int urlIDNumber = tud.getNextURLNumber();
java.net.URL urlToDownload=new
java.net.URL("http://www.google.com");
try {
urlToDownload = tud.getURL(urlIDNumber);
} catch (ArrayIndexOutOfBoundsException aob) {
break;
}
java.net.HttpURLConnection connection =
(java.net.HttpURLConnection) urlToDownload.openConnection();
String contentType = connection.getContentType();
int responseCode;
try {
responseCode = connection.getResponseCode();
} catch (java.io.IOException e) {
content="Connection timed out";
responseCode=408;
}
if(responseCode==200) {
java.io.InputStream in = ((java.io.InputStream)
connection.getContent());
java.io.BufferedReader dis = new
java.io.BufferedReader(new java.io.InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String line;
while ((line=dis.readLine()) != null) {
sb.append(line+"\n");
}
content = sb.toString();
} else {
content="No content: response was "+ responseCode;
}
tud.setContent(content,urlIDNumber);
tud.setContentType(contentType,urlIDNumber);
tud.setResponseCode(responseCode, urlIDNumber);
connection.disconnect();
connection=null;
}
} catch (java.io.IOException e) {
e.printStackTrace();
} catch (FinishedException e) {


}

}
}
</LONG NASTY HACKY CODE>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Threads - Get running threads Pedro Pinto Java 2 04-08-2008 11:44 PM
[new to threads] threads with UI and loop Une bévue Ruby 0 06-14-2006 10:22 AM
TB View, Threads, Threads with unread The Invisible Man Firefox 1 03-20-2006 02:09 AM
Standard Threads vs Weightless Threads yoda Python 2 08-01-2005 09:12 PM
threads without threads sindica@gmail.com C Programming 4 08-27-2004 09:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57