java - infinite loop database check -


i'm using jdbc, need check database against changing values.

what have infinite loop running, inner loop iterating on changing values, , each iteration checking against database.

public void runinbg() { //this method called thread     while(true) {      while(els.haselements()) {       test el = (test)els.next();        string sql = "select * test id = '" + el.getid() + "'";        record r = db.gettestrecord(sql);//this function makes connection, executequery etc...and return record object values        if(r != null) {          //do        }      }     } } 

i'm think isn't best way.

the other way i'm thinking reverse, keep iterating on database.

update

thank feedback regarding timers, don't think solve problem. once change occurs in database need process results instantaneously against changing values ("els" example code).

even if database not change still has check against changing values.

update 2

ok, interested in answer believe have solution now. solution not use database this. load in, update, add, etc... whats needed database memory. way don't have open , close database constantly, deal database when make change it, , reflect changes memory , deal whatever in memory @ time. sure more memory intensive performance absolute key here.

as periodic "timer" answers, i'm sorry not right @ all. nobody has responded reason how use of timers solve particular situation.

but thank again feedback, still helpful nevertheless.

another possibility using scheduledthreadpoolexecutor.

you implement runnable containing logic , register scheduledexecutorservice follows:

scheduledthreadpoolexecutor executor = new scheduledthreadpoolexecutor(10); executor.scheduleatfixedrate(myrunnable, 0, 5, timeunit.seconds); 

the code above, creates scheduledthreadpoolexecutor 10 threads in pool, , have runnable registered run in 5 seconds period starting immediately.


to schedule runnable use:

scheduleatfixedrate

creates , executes periodic action becomes enabled first after given initial delay, , subsequently given period; executions commence after initialdelay initialdelay+period, initialdelay + 2 * period, , on.

schedulewithfixeddelay

creates , executes periodic action becomes enabled first after given initial delay, , subsequently given delay between termination of 1 execution , commencement of next.


and here can see advantages of threadpoolexecutor, in order see if fits requirements. advise question: java timer vs executorservice? in order make decision.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -