无限循环数据库检查 [英] infinite loop database check

查看:108
本文介绍了无限循环数据库检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JDBC,需要经常检查数据库以防止更改值。

I'm using JDBC, need to constantly check the database against changing values.

我目前所拥有的是无限循环运行,内部循环迭代变化值和每次迭代检查数据库。

What I have currently is an infinite loop running, inner loop iterating over a changing values, and each iteration checking against the database.

public void runInBG() { //this method called from another thread
    while(true) {
     while(els.hasElements()) {
      Test el = (Test)els.next();
       String sql = "SELECT * FROM Test WHERE id = '" + el.getId() + "'";
       Record r = db.getTestRecord(sql);//this function makes connection, executeQuery etc...and return Record object with values
       if(r != null) {
         //do something
       }
     }
    }
}

我认为这不是最好的方式。

I'm think this isn't the best way.

我想的另一种方式是反过来,继续迭代数据库。

The other way I'm thinking is the reverse, to keep iterating over the database.

更新

感谢您提供有关计时器的反馈,但我认为这不会解决我的问题。
一旦数据库发生变化,我需要几乎立即处理结果与变化的值(示例代码中的els)。

Thank you for the feedback regarding timers, but I don't think it will solve my problem. Once a change occurs in the database I need to process the results almost instantaneously against the changing values ("els" from the example code).

即使数据库没有改变它仍然必须经常检查更改的值。

Even if the database does not change it still has to check constantly against the changing values.

更新2

好的,对任何对答案感兴趣的人,我相信我现在有解决方案。基本上解决方案是不要使用数据库。加载,更新,添加等...仅从数据库到内存需要什么。
这样你不必经常打开和关闭数据库,你只需要对数据库进行更改就可以处理数据库,并将这些更改反映到内存中,并且只处理内存中的任何内容。时间。
当然这是更大的内存密集型,但性能绝对是关键。

OK, to anyone interested in the answer I believe I have the solution now. Basically the solution is NOT to use the database for this. Load in, update, add, etc... only whats needed from the database to memory. That way you don't have to open and close the database constantly, you only deal with the database when you make a change to it, and reflect those changes back into memory and only deal with whatever is in memory at the time. Sure this is more memory intensive but performance is absolute key here.

关于定期的计时器答案,我很抱歉,但这不对一点都不没有人有理由如何使用计时器来解决这种特殊情况。

As to the periodic "timer" answers, I'm sorry but this is not right at all. Nobody has responded with a reason how the use of timers would solve this particular situation.

但是再次感谢您的反馈,它仍然有用。

But thank you again for the feedback, it was still helpful nevertheless.

推荐答案

另一种可能性是使用 ScheduledThreadPoolExecutor

Another possibility would be using ScheduledThreadPoolExecutor.

您可以实现包含逻辑的 Runnable 并将其注册到 ScheduledExecutorService ,如下所示:

You could implement a Runnable containing your logic and register it to the ScheduledExecutorService as follows:

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
executor.scheduleAtFixedRate(myRunnable, 0, 5, TimeUnit.SECONDS);

上面的代码创建了一个 ScheduledThreadPoolExecutor 它的池中有10个线程,并且注册了一个 Runnable ,它将在5秒内立即开始运行。

The code above, creates a ScheduledThreadPoolExecutor with 10 Threads in its pool, and would have a Runnable registered to it that will run in a 5 seconds period starting immediately.

要安排运行,您可以使用:

scheduleAtFixedRate


创建并执行定期操作在给定的初始延迟之后首先启用,然后在给定的时间段内启用;执行将在initialDelay之后开始,然后是initialDelay + period,然后是initialDelay + 2 * period,依此类推。

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on.

scheduleWithFixedDelay


创建并执行定期操作在给定的初始延迟之后首先启用,然后在一次执行终止和下一次执行开始之间给定延迟。

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay between the termination of one execution and the commencement of the next.



< hr>

这里您可以看到 ThreadPoolExecutor 的优点,以确定它是否符合您的要求。我建议这个问题: Java Timer vs ExecutorService?也是为了做出正确的决定。


And here you can see the advantages of ThreadPoolExecutor, in order to see if it fits to your requirements. I advise this question: Java Timer vs ExecutorService? too in order to make a good decision.

这篇关于无限循环数据库检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆