multithreading - Implement Multi-threading on Java Program -


i'm writing little java program uses psexec.exe cmd launched using processbuilder copy , install application on networked pc (the number of pc need installed can vary 5 50).

the program works fine if launched processbuilder each pc sequentially.

however speed things implement form of multithreading allow me install 5 pc's @ time concurrently (one "batch" of 5 x processbuilder processes untill pc's have been installed).

i thinking of using fixed thread pool in combination callable interface (each execution of psexec returns value indicates if execution succesfull , have evaluate).

the code used processbuilder is:

            // start iterating on pc in list:             for(string pc : pclist)             {                 counter++;                  logger.info("starting installation of remote pc: " + pc);                  updatemessage("starting installation of remote pc: " + pc);                    int exitval = 99;                                     logger.debug("exit value set 99");                  try                  {                                             processbuilder pb = new processbuilder();                     pb.command("cmd", "/c",                              "\""+psexecpath+"\"" + " \\\\" + pc + username + userpassword + " -c" + " -f" + " -h" + " -n 60 " +                                      "\""+forumviewerpath+"\"" + " -q "+ forumaddress + remotepath + "-overwrite");                                              logger.debug(pb.command().tostring());                       pb.redirecterror();                     process p = pb.start();                     inputstream sterr = p.geterrorstream();                     inputstreamreader esr = new inputstreamreader(sterr);                     bufferedreader bre = new bufferedreader(esr);                      string line = null;                      line = bre.readline();                      while (line != null)                     {                         if(!line.equals(""))                             logger.info(line);                                         line = bre.readline();                     }                     exitval = p.waitfor();                 } catch (ioexception ex)                  {                     logger.info("exception occurred during installation of pc: \n"+pc+"\n "+ ex);                     notinstalledpc.add(pc);                 }                  if(exitval != 0)                 {                     notinstalledpc.add(pc);                     ret = exitval;                     updatemessage("");                                         updatemessage("the remote pc: " + pc + " not installed");                      logger.info("the remote pc: " + pc + " not installed. error message returned was: \n"+geterror(exitval) + "\nprocess exit code was: " + exitval);                 }                 else                 {                     updatemessage("");                                         updatemessage("the remote pc: " + pc + " succesfully installed");                                               logger.info("the remote pc: " + pc + " succesfully installed");                                                                   } 

now i've read info on how implement callable , enclose processbuilder in callable interface , submit tasks running in loop.

am on right track?

you can surely that. suppose want use callable instead of runnable result of exitval ?

it doesn't seem have shared data between threads, think should fine. since know how many callables going make create collection of callables ,

list<future<sometype>> results = pool.invokeall(collection)  

this allow easier handling of result. important thing need figure out when deciding on whether or not use threadpool if program terminates while threads still running; have finish you're doing in threads, need have seamless handling of errors etc.

check out java threadpools doc: https://docs.oracle.com/javase/tutorial/essential/concurrency/pools.html or search web, there tons of posts/blogs when or not use threadpools.

but seems you're on right track!


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -