multithreading - Java - Close a thread with 'return' not working -


i have following code in class contains main method

executorservice executor = executors.newfixedthreadpool(1); runnable formatconcentration = new formatconcentration(87); executor.execute(formatconcentration); system.out.println("called instance of formatconcentration"); while (!executor.isterminated()) {     //stay alive     thread.sleep(1000);     system.out.println("still alive"); } system.out.println("program finished"); return; 

this creates instance of formatconcentration class. code following (i've taken of functionality out sake of example).

public class formatconcentration extends thread{     private int numberofnewrows;      formatconcentration(int rows)     {         this.numberofnewrows = rows;     }     public void run() {          system.out.println("running formatting script");         final int numberofnewregistered = this.numberofnewrows;         try          {             system.out.println("finished formatting");         }//end of try         catch (exception e1)          {             log.log(e1.tostring());             log.closelog() ;             system.exit(1) ;         }         system.out.println("still finished formatting");         return;     } } 

my problem once return invoked not terminate thread.

i have done quite lot of looking around , far can tell should fine imagine i'm overlooking small , appreciate fresh eyes on problem.

or if has suggestion of how kill thread inside run(){} method appreciate (preferably not setting variable i'll check in main class if that's how have it) know once reaches return statement it's finished , no longer need reference variables created in run().

the output generated console follows:

called instance of formatconcentration running formatting script finished formatting still finished formatting still alive still alive still alive still alive still alive etc. 

executorservice executor = executors.newfixedthreadpool(1); runnable formatconcentration = new formatconcentration(87); executor.execute(formatconcentration); system.out.println("called instance of formatconcentration"); executor.shutdown(); while (!executor.isterminated()) {     //stay alive     thread.sleep(1000);     system.out.println("still alive"); } system.out.println("program finished"); return; 

another simpler way :

   executorservice executor = executors.newfixedthreadpool(1);     runnable formatconcentration = new formatconcentration(87);     executor.execute(formatconcentration);     executor.shutdown();     try {         executor.awaittermination(long.max_value, timeunit.nanoseconds);     } catch (interruptedexception e) {         // todo auto-generated catch block         e.printstacktrace();     } 

executor awaittermination method substitutes loop.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -