java - Why SocketTimeoutException makes my program freeze? -


i give program on 600 links, stocked in arraylist title of webpage, using jsoup (among others). every link (using for loop), create new thread (using thread.start()) , pass link through program, wait thread finish (with thread.join) before launching new thread (simultaneous execution cause problems, did prevent unexpected thread end stop execution other links).

the problem sometimes, jsoup throws sockettimeoutexception (which i'm supposed catch), makes program freeze. don't know why execution stops surrounded try/catch.

here's piece of code, maybe understand :

// in method actionperformed() of jpanel  for(final string link : links) {     thread t = new thread()     {         public void run()         {             analyzer.process(link);         }     };     t.start();     try      {         t.join();     }      catch (interruptedexception e)      {         e.printstacktrace();     } } 

and in process :

// method process() of analyzer class try  {     document doc = jsoup.connect(lien).useragent("mozilla").timeout(5*10000).get();      //                    ^ exception thrown here ! ^      title = doc.title(); } catch (exception e)  {     e.printstacktrace();     erreurs+="erreur lors de la lecture du titre\n"; } 

it's annoying because process veeery long, let run night, , find today program froze @ 54th link. ^^' thank in advance !

edit - update

sercanozdemir suggested me use executorservice, instead of creating threads , making start()-join(), tried :

executorservice ex = executors.newsinglethreadexecutor(); for(final string link : links) {     system.err.println("-- "+i+" --");              //debug     ex.execute(new runnable(){             @override             public void run(){                 try                 {                     analyzer.process(link);                 }                 catch( exception e )                 {                     e.printstacktrace();                 }              }     });     i++;                                            //debug     } ex.shutdown(); 

but prints debug links. idea of why doesn't run process ?

i've not tried jsoup simple way of creating threads task , monitoring status.

    executorservice executorservice = executors.newcachedthreadpool();     future future = null;     for(final string link : links)     {         future = executorservice.submit(new runnable(){          @override         public void run(){             try{                analyzer.process(link);             }             catch( exception e ){                 e.printstacktrace();             }          }     });          while(future != null                 && !future.isdone()                 && !future.iscancelled())         {             try {                 thread.sleep(2000); // or else             } catch (interruptedexception e) {                 e.printstacktrace();             }         }      }      executorservice.shutdown(); 

the code in updated question won't work because creating service , instantly overwriting without waiting complete.

executorservice ex = executors.newsinglethreadexecutor(); for(final string link : links) {   system.err.println("-- "+i+" --");              //debug   ex.execute(new runnable(){ //here created         @override         public void run(){             try             {                 analyzer.process(link);             }             catch( exception e )             {                 e.printstacktrace();             }          } }); i++;                                            //debug //a split second later loop finishes , overwrites service again } ex.shutdown(); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -