首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

请问一个多线程中关于临时变量的有关问题

2012-12-20 
请教一个多线程中关于临时变量的问题!情况一:2个线程 不同的业务 调用的是不同方法 不同方法中使用的都是f

请教一个多线程中关于临时变量的问题!
情况一:2个线程 不同的业务 调用的是不同方法 不同方法中使用的都是for(int i,,i++)这种写法 想请教-->这样不同的线程运行时候 线程1的i的循环赋值是否会影响到线程2的赋值!

情况二:两个线程 相同的业务 调用相同方法 两个线程启动运行 想请教-->线程1的i的循环赋值是否会影响到线程2的赋值!

现在在情况一下 线程1执行一个循环 i的赋值会突然被被赋值为0 所以很是纠结 因为业务需求 给方法上线程锁就没有意义了 
[最优解释]

引用:
╮(╯▽╰)╭  现在debug的情况就是 线程1的循环已经走到了i=3了 但是当另外一个线程启动时 线程1的i就变成等0了 问题是 这个两个线程调用的都是不同的方法啊 这样也会串到啊。。。

两个线程操作的都是不同的变量(可以确定吧?)怎么会影响到呢? 确认一下是不是由该线程引起的变化,或者给出相关代码。
[其他解释]
首先声明,俺对线程的了解全都是理论上,没有实际在工作中用到过,用到的都是简单的情况。

情况一中,按说应该不会的,没有看具体代码也不知道你后面说的是什么原因。局部不变量有自己的作用域,超过作用域是引用不到的。
是否可以debug一下看看线程的每个步骤里面的调用。
[其他解释]
╮(╯▽╰)╭  现在debug的情况就是 线程1的循环已经走到了i=3了 但是当另外一个线程启动时 线程1的i就变成等0了 问题是 这个两个线程调用的都是不同的方法啊 这样也会串到啊。。。
[其他解释]
引用:
首先声明,俺对线程的了解全都是理论上,没有实际在工作中用到过,用到的都是简单的情况。

情况一中,按说应该不会的,没有看具体代码也不知道你后面说的是什么原因。局部不变量有自己的作用域,超过作用域是引用不到的。
是否可以debug一下看看线程的每个步骤里面的调用。


很少用线程。。
[其他解释]
正在学习中.
[其他解释]
引用:
引用:╮(╯▽╰)╭  现在debug的情况就是 线程1的循环已经走到了i=3了 但是当另外一个线程启动时 线程1的i就变成等0了 问题是 这个两个线程调用的都是不同的方法啊 这样也会串到啊。。。
两个线程操作的都是不同的变量(可以确定吧?)怎么会影响到呢? 确认一下是不是由该线程引起的变化,或者给出相关代码。
        ……

public boolean saveSourceDataXML(Integer sourceTableId, Integer destTableId) throws Exception {
initConfig();
SynLogDAO synLogDAO = (SynLogDAO) SpringBeanUtil.getBean("synLogDAO");
MetaTable sourceTable = null; //源数据表对象
MetaTable destTable = null; //目标表对象
List<FieldsMap> userMapping = null; //用户映射字段集合
List<MetaField> sourceFields = null; //源数据表映射字段集合
List<MetaField> destFields = null; //目标表映射字段集合

sourceTable = (MetaTable) getModelDAO().getById(sourceTableId, MetaTable.class);
destTable = (MetaTable) getModelDAO().getById(destTableId, MetaTable.class);
userMapping = getUserMapping(sourceTableId, destTableId);
sourceFields = new ArrayList<MetaField>();
destFields = new ArrayList<MetaField>();
for (FieldsMap f : userMapping) {
sourceFields.add((MetaField) getModelDAO().getById(f.getFieldIdSource(), MetaField.class));
destFields.add((MetaField) getModelDAO().getById(f.getFieldIdDest(), MetaField.class));
}
DataSourceConfig sourceDataSourceConfig = (DataSourceConfig) swapDataDAO.getById(sourceTable
.getDatasourceId(), DataSourceConfig.class);
swapDataDAO.inintConnection(sourceDataSourceConfig.getDriver(), sourceDataSourceConfig.getUrl(),
sourceDataSourceConfig.getUsername(), sourceDataSourceConfig.getPassword());
BatchPo batchPo = new BatchPo();
batchPo.setCount(swapDataDAO.getSwapDatasCount(sourceTable));
int count = batchPo.getCount() / batchPo.getSize() + 1;


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时一并打印下该数值看看)
[其他解释]

引用:
>>System.out.println("总共执行" + count + "回合______当前在第" + j + "回合__________上次执行>>到第" + q


>>+ "条________当前返回集合长度" + swapDatas.size());

变成0的是这里的j吗?
线程1是怎么在3个循环后停下来的呢?(可以在该打印语句前再设一个静态变量的自增……


...因为我断点打在那等着。。。我让这帮先执行3次 然后停住 等另外个线程启动 然后 在执行次循环 i就被变成了0 了。。。

[其他解释]
引用:
>>System.out.println("总共执行" + count + "回合______当前在第" + j + "回合__________上次执行>>到第" + q
>>+ "条________当前返回集合长度" + swapDatas.size());

变成0的是这里的j吗?
线程1是怎么在3个循环后停下来的呢?(可以在该打印语句前再设一个静态变量的自增……


大神!有木有空,发个私信给你 私聊下!我现在很是纠结这问题 debug半天都找不出原因!
[其他解释]
引用:
...因为我断点打在那等着。。。我让这帮先执行3次 然后停住 等另外个线程启动 然后 在执行次循环 i就被变成了0 了。。。

哦 对于debug下为何是这种表现不清楚,但线程在debug模式下和实际运行情况会有所不同的。

实际运行时j在一次循环内也会变零?

热点排行