从同一个表读取的两个线程:如何使两个线程不从TASKS表中读取同一组数据 [英] Two threads reading from the same table:how do i make both thread not to read the same set of data from the TASKS table

查看:226
本文介绍了从同一个表读取的两个线程:如何使两个线程不从TASKS表中读取同一组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务线程在两个独立的tomcat实例中运行。
任务线程在某些where条件下同时读取(使用select)TASKS表,然后进行一些处理。

I have a tasks thread running in two separate instances of tomcat. The Task threads concurrently reads (using select) TASKS table on certain where condition and then does some processing.

问题是,有时两个线程都选择相同的任务,因为任务执行两次。
我的问题是如何使两个线程不从TASKS表中读取相同的数据集

Issue is ,sometimes both the threads pick the same task , because of which the task is executed twice. My question is how do i make both thread not to read the same set of data from the TASKS table

推荐答案

我认为您需要有一些变量(列),您可以在其中保留行的最后修改日期。您的线程可以使用相同的修改日期限制读取相同的数据集。

编辑:
我没看到不读

在这种情况下,您需要另一个表TaskExecutor(taskId,executorId),当某个线程运行任务时,您放置数据到TaskExecutor;当你启动另一个线程时,它只检查该任务是否已经执行(从RanTask中选择...,其中taskId = ...)。
您还需要处理转换的隔离级别。

In this case you need have another table TaskExecutor (taskId , executorId) , and when some thread runs task you put data to TaskExecutor; and when you start another thread it just checks that task is already executing or not (Select ... from RanTask where taskId = ...). Нou also need to take care of isolation level for transaсtions.

这篇关于从同一个表读取的两个线程:如何使两个线程不从TASKS表中读取同一组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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