public static void downloadFromUrlsFile ( String dowloadFolder , String urlsFile , int numUrls , int urlsToSkip ) throws Exception { long start = System . currentTimeMillis ( ) ; int numThreads = 10 ; BufferedReader in = new BufferedReader ( new FileReader ( new File ( urlsFile ) ) ) ; for ( int i = 0 ; i < urlsToSkip ; i ++ ) { in . readLine ( ) ; } ImageDownloader downloader = new ImageDownloader ( dowloadFolder , numThreads ) ; int submittedCounter = 0 ; int completedCounter = 0 ; int failedCounter = 0 ; String line = "" ; while ( true ) { String url ; String id = "" ; // if there are more task to submit and the downloader can accept more tasks then submit while ( submittedCounter < numUrls && downloader . canAcceptMoreTasks ( ) ) { line = in . readLine ( ) ; url = line . split ( "\\s+" ) [ 1 ] ; id = line . split ( "\\s+" ) [ 0 ] ; downloader . submitImageDownloadTask ( url , id ) ; submittedCounter ++ ; } // if are submitted taks that are pending completion ,try to consume if ( completedCounter + failedCounter < submittedCounter ) { try { downloader . getImageDownloadResultWait ( ) ; completedCounter ++ ; System . out . println ( completedCounter + " downloads completed!" ) ; } catch ( Exception e ) { failedCounter ++ ; System . out . println ( failedCounter + " downloads failed!" ) ; System . out . println ( e . getMessage ( ) ) ; } } // if all tasks have been consumed then break; if ( completedCounter + failedCounter == numUrls ) { downloader . shutDown ( ) ; in . close ( ) ; break ; } } long end = System . currentTimeMillis ( ) ; System . out . println ( "Total time: " + ( end - start ) + " ms" ) ; System . out . println ( "Downloaded images: " + completedCounter ) ; System . out . println ( "Failed images: " + failedCounter ) ; }
This method exemplifies multi - threaded image download from a list of urls . It uses 5 download threads .