请教一个多线程中关于临时变量的问题!
情况一:2个线程 不同的业务 调用的是不同方法 不同方法中使用的都是for(int i,,i++)这种写法 想请教-->这样不同的线程运行时候 线程1的i的循环赋值是否会影响到线程2的赋值!
情况二:两个线程 相同的业务 调用相同方法 两个线程启动运行 想请教-->线程1的i的循环赋值是否会影响到线程2的赋值!
现在在情况一下 线程1执行一个循环 i的赋值会突然被被赋值为0 所以很是纠结 因为业务需求 给方法上线程锁就没有意义了
[最优解释]
for (int j = 0; j < count; j++) {
batchPo.setStartIndex(j * batchPo.getSize());
int q = batchPo.getStartIndex();
List<SwapData> swapDatas = swapDataDAO.getSwapDatas(sourceFields, destFields, sourceTable, destTable,
batchPo);
System.out.println("总共执行" + count + "回合______当前在第" + j + "回合__________上次执行到第" + q
+ "条________当前返回集合长度" + swapDatas.size());
//如果源数据表没有数据 则直接返回!不进行操作
if (swapDatas == null
[其他解释]
swapDatas.size() == 0) {
continue;
}
ParseToXML ptx = new ParseToXML();
ptx.setTemplatePath(templatePath);
ptx.setTableName(destTable.getEname());
// 生成smooks模板
ptx.setTemplateName(smooksTemplateName);
ptx.setTargetPath(smooksTargetPath + destTable.getCreatorDeptId() + "_" + destTable.getDatasourceId()
+ "_" + ptx.getTableName() + "_smooksTemplate.xml");
File file = new File(ptx.getTargetPath());
Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
ptx.parseDataSourceToXML(swapDatas.get(0), destTable.getEname(), out);
out.close();
ptx.setTableName(sourceTable.getEname());
ptx.setTemplateName(templateName);
for (int i = 0; i < swapDatas.size(); i++) {
boolean flag = synLogDAO.verdictSynLog(swapDatas.get(i).getRecordIdSource(), swapDatas.get(i)
.getSourceTableId(), swapDatas.get(i).getDestTableId());
if (!flag) {
continue;
}
// 生成源数据xml
String sourceXmlName = targetPath + sourceTable.getCreatorDeptId() + "_"
+ sourceTable.getDatasourceId() + "_" + ptx.getTableName() + "_"
+ (i + batchPo.getStartIndex()) + "_source.xml";
ptx.setTargetPath(sourceXmlName);
file = new File(ptx.getTargetPath());
SwapData swapData = swapDatas.get(i);
out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
ptx.parseDataSourceToXML(swapData, 0, 1, out);
out.close();
}
}
return true;
}
[其他解释]
public ResultMsg saveDestData(Integer destTableId, Integer count, Integer sourceTableId) throws Exception {
initConfig();
ResultMsg rsm = null;
MetaTable destTable = null; //目标表对象
MetaTable sourceTable = null; //源数据表对象
sourceTable = (MetaTable) getModelDAO().getById(sourceTableId, MetaTable.class);
destTable = (MetaTable) getModelDAO().getById(destTableId, MetaTable.class);
DataSourceConfig destDataSourceConfig = (DataSourceConfig) swapDataDAO.getById(
destTable.getDatasourceId(), DataSourceConfig.class);
swapDataDAO.inintConnection(destDataSourceConfig.getDriver(), destDataSourceConfig.getUrl(),
destDataSourceConfig.getUsername(), destDataSourceConfig.getPassword());
ParseXML px = new ParseXML();
File parseFiles = new File(smookiedTargetPath);
String[] parseFile = (String[]) parseFiles.list();
List<SwapData> destSwapDatas = new ArrayList<SwapData>();
String smooksTemplate = smooksTargetPath + destTable.getCreatorDeptId() + "_"
+ destTable.getDatasourceId() + "_" + destTable.getEname() + "_smooksTemplate.xml";
int row = 0;
String threadId = String.valueOf(Thread.currentThread().getId());
File newDirectory = new File(targetPath + threadId);
newDirectory.mkdir();
String sourceTableName = sourceTable.getCreatorDeptId() + "_" + sourceTable.getDatasourceId() + "_"
+ sourceTable.getEname();
for (int t = 0; t < parseFile.length; t++) {
String fileName = parseFile[t];
if (row == 150) {
break;
} else {
if (fileName.startsWith(sourceTableName) && fileName.endsWith("_source.xml")) {
File file = new File(targetPath + fileName);
File newFile = new File(newDirectory.getPath() + "/" + fileName);
file.renameTo(newFile);
row++;
}
}
}
String[] parseXML = newDirectory.list();
for (int t = 0; t < parseXML.length; t++) {
String fileName = parseFile[t];
String[] s = fileName.split("_");
XmlParseToXml xptx = new XmlParseToXml();
byte[] messageIn = xptx.readInputMessage(newDirectory.getPath() + "/" + fileName, "utf-8");
String messageOut = xptx.runSmooks(smooksTemplate, messageIn);
String path = newDirectory.getPath() + "/" + destTable.getCreatorDeptId() + "_"
+ destTable.getDatasourceId() + "_" + destTable.getEname() + "_" + s[3] + "_dest.xml";
File file = new File(path);
StreamUtils.writeFile(file, messageOut.getBytes("utf-8"));
SwapData swapData = px.parseXML(path);
destSwapDatas.add(swapData);
System.gc();
file.delete();
file = new File(newDirectory.getPath() + "/" + fileName);
file.delete();
}
newDirectory.delete();
rsm = swapDataDAO.saveDestData(destSwapDatas);
rsm.setReObj(((Integer) rsm.getReObj() + count));
rsm.setFlag(true);
return rsm;
}
[其他解释]
saveSourceDataXML 是线程1调用的方法 saveDestData 是线程2调用的方法 我先启动的线程1的方法 执行3个循环后 停住等线程2启动 然后
System.out.println("总共执行" + count + "回合______当前在第" + j + "回合__________上次执行到第" + q
+ "条________当前返回集合长度" + swapDatas.size());
根据这里可以看到我的执行回合又变成了0;
[其他解释]
>>System.out.println("总共执行" + count + "回合______当前在第" + j + "回合__________上次执行>>到第" + q
>>+ "条________当前返回集合长度" + swapDatas.size());
变成0的是这里的j吗?
线程1是怎么在3个循环后停下来的呢?(可以在该打印语句前再设一个静态变量的自增计数器,打印j时一并打印下该数值看看)
[其他解释]