我有一个适用于其中的代码,each thread
它将10 minutes
决定我应该选择哪个表random number
。然后在那之后我SQL query
使用PreparedStatement
. result set
执行完之后ResultSet
,我需要遍历List<String> colData
.
此处columnsList
将包含table columns
以逗号分隔的内容。例如-
col1, col2, col3
下面是代码-
class ReadTask implements Runnable {
private static Random random = new SecureRandom();
public ReadTask() {
}
@Override
public run() {
...
while ( < 10 minutes) {
double randomNumber = random.nextDouble() * 100.0;
ReadTableConnectionInfo tableInfo = selectRandomConnection(randomNumber);
final int id = generateRandomId(random);
final String columnsList = getColumns(table.getColumns());
final String selectSql = "SELECT " + columnsList + " from " + table.getTableName() + " where id = ?";
preparedStatement = tableStatement.get(table.getTableName()).prepareCall(selectSql);
preparedStatement.setString(1, String.valueOf(id));
rs = preparedStatement.executeQuery();
List<String> colData = new ArrayList<String>(columnsList.split(",").length);
boolean foundData = false;
if (id >= 1 && id <= 5000) {
if (rs.next()) {
foundData = true;
for (String column : columnsList.split(",")) {
colData.add(rs.getString(column));
}
rs.next();//this should return false or an error condition and do I need this here?
}
} else if (rs.next()) {
// do some stuff
}
if (flagValidateData && foundData) {
// iterate through colData map
}
}
}
}
问题陈述:-
1)我是否需要同步colData list
?
2)我添加数据的方式是否List<String> colData
是线程安全的?
3)在我循环结果集并将其添加到的方式中是否还有其他问题colData string array
?鉴于此,它是一个多线程代码,因此很难针对任何竞争条件对其进行调试。