multithreading - Use threads on a RESTful Web Service in Java -


everyone. have problem restful web service, have client in php call restful service in java. in restful service have post method, executes update query modify rows in table 10000 records . want use threads this. possible that?. please me i'm kind of new in java. thanks.

ok i'm doing in service layer:

for(int = 0; < 10; i++) {     startrow = i*1000;     finalrow = i*1000 + 1000;     runnable process = new processrecords(startrow , finalrow);     executor.execute(process);                     }  // wait until threads finish while (!executor.isterminated()) {  } system.out.println("\n threads finished"); 

and i'm calling class (processrecords) execute update:

public processrecords (int start, int final) {     startrow = start;     finalrow = final; }  @override public void run(){      try {          consultuniversity consult = new consultuniversity ();         consult.averangegrade(startrow, finalrow);      } catch (exception ex) {         logger.getlogger(procesos.class.getname()).log(level.severe, null, ex);     } } 

then in data layer i'm doing inside of method "averangegrade":

 try {         conn = universityconection();//this conection          query = "select * grades limit " + filainicial + "," + filafinal;                     prepstatement = conn.preparestatement(query);          rs = prepstatement.executequery(query);            rsmd = rs.getmetadata();          while(rs.next())         {             averange = (rs.getint("nfirgrd") + rs.getint("nsecgrd") +                                 rs.getint("nthrgrd"))/3; //the averange of 3 grades              query = "update grades set nfingrd = ? ccodalu = ?";              prepstatement = conn.preparestatement(query);             prepstatement.setint(1, averange);             prepstatement.setstring(2, rs.getstring("ccodalu"));              prepstatement.executeupdate();              system.out.println("record " + rs.getstring("ccodalu") +                                         " modified");         }                         conn.close(); //close connection          } 

then when execute cliente, service update top rows, 50 rows, returns message, if processes finished, , don't know why. think not waiting until threads finish, thre code :s, why happening?. please me. thank you.

sure, possible. familiar concurrent api offered java?

at high level point of view, have write code handles http post. in code, can instantiate arbitrary number of threads (a thread pool), , each thread of pool makes update on subset of rows update. example can start 10 threads , each thread make update on 1000 rows.

furthermore:

  1. i setup thread pool @ startup of application, every time post executed uses created pool, , not instantiate new 1 every time (it expensive)
  2. to assign subset of rows each thread, have use limit , start sql clauses, let select subset. each thread have different query based on 2 parameters

in code:

// instantiate pool executorservice pool=executors.newfixedthreadpool(poolsize); // run task execute in parallel, specificying subset of rows pool.execute(new updatehandler(limit,skip));  // below find prototype of async thread task class handler implements runnable {    private final int skip;    private final int limit;    handler(int limit, int skip) { ... }     public void run() {      // specify code runs query here    }  } 

you can find documentation here or here hope helps.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -