Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Stop thread if it takes too long

807605Aug 24 2007 — edited Aug 24 2007
I have written a program which launches a new thread and within this thread it attempts to download a webpage at a specified URL. I use the "join" method to ensure the thread finishes before the program can move on.

If the server hosting the page hangs or for some other reason the page takes a significant time to download I want to be able to "cancel" the thread and move on.

The code is as follows:
// Create a new crawl object
Crawl newCrawlObj;
newCrawlObj = new Crawl();
Thread newCrawlThread = new Thread(newCrawlObj);
newCrawlThread.start();
// Wait until the Crawl thread has finished before starting another
try {newCrawlThread.join();}
catch (InterruptedException ex) {ex.printStackTrace();}
So, for arguments sake, is there a way to say "if the thread takes longer than 20 seconds "cancel" the thread and continue execution?

Thanks
-Myles
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2007
Added on Aug 24 2007
10 comments
387 views