[英]Passing values in runnable class constructor in Java
I hope somebody could please help me out in identifying to where I am going wrong with the codes below. 我希望有人可以帮助我确定下面的代码在哪里出了问题。
public class CCFileImpl implements CCFileAbs {
private LogMe logMe = null;
private ExecutorService ccfileimpl_exsc = null;
private CCProcessorImpl cProc = null;
private DataUtil dUtil = null;
public CCFileImpl() {
this.logMe = LogMe.getLogger();
if (dUtil == null) {
dUtil = new DataUtil();
}
}
@Override
public void getFilesForProcess() {
CCHeader cHead = null;
Future future = null;
String sPath = PropReader.getPropValue(PropReader.FILEDIR); //D:\samples\
int iCtr = 0;
ccfileimpl_exsc = Executors.newFixedThreadPool(Integer.parseInt(PropReader.getPropValue(PropReader.TPool_File)));
Date dToday = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Iterator iter = dUtil.getFilesForProcess(sdf.format(dToday)).iterator();
String sFileGroupName = "", sFileName = "";
String sId = null; //"testFiles";
while (iter.hasNext()) {
cHead = (CCHeader) iter.next();
sFileName = cHead.getsFileName(); //(String) iter.next();
sId = cHead.getsId();
sFileGroupName = sFileName + "_" + iCtr++;
dUtil.updTPDHDRStatusById(sId); //Interface utility class // <=== And also here, when trying to update the db
// nothing happened.
cProc = new CCProcessorImpl(sId, sFileGroupName, sPath, sFileName); // <=== Problem is here?
future = ccfileimpl_exsc.submit(cProc);
}
ccfileimpl_exsc.shutdown();
}
}
The above code retrieves the files for processing then assigning it to a runnable class (below) then submitting it to an executorService class. 上面的代码检索文件进行处理,然后将其分配给可运行类(如下),然后将其提交给executorService类。
Now i can't understand to why the passed values of the constructor (below) is set to null/space and only the sPath variable has a definite value. 现在,我不明白为什么将构造函数的传递值(以下)设置为null / space,而只有sPath变量具有确定的值。
public class CCProcessorImpl implements Runnable{
private CCFileParser rpsCCParser;
private ExecutorService ccprocimpl_exsc;
private static LogMe logMe;
private final String sGroupName;
private final String sId;
private final String sFileName;
@Override
public void run() {
this.parseFiles(sId, sFileName);
}
public CCProcessorImpl(String sId, String sGroupName, String sPath, String sFileName) {
this.logMe = LogMe.getLogger();
this.sId = sId;
this.sGroupName = sGroupName;
this.sFileName = sPath + sFileName;
}
public void parseFiles(String sId, String sFileName) {
try {
Future future = null;
rpsCCParser = new CCFileParser(sId, sFileName);
ArrayList aList = rpsCCParser.getFileContent();
String sGroupName = sId + "_";
ccprocimpl_exsc = Executors.newFixedThreadPool(Integer.parseInt(PropReader.getPropValue(PropReader.TPool_Content)));
int iStart = 0, iSize = 9, iEnd = iSize;
for (int iCtr = 0; iCtr <= ((aList.size() / 10) - 1); iCtr++, iStart += iSize, iEnd += iSize) {
future = ccprocimpl_exsc.submit(new CCUpdater(aList.subList(iStart, iEnd), sGroupName + iCtr));
}
future.get();
ccprocimpl_exsc.shutdown();
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
}
Also as a supplementary question, why is it when i tried to update the db table no updates were performed? 另外,作为补充问题,为什么在我尝试更新数据库表时未执行任何更新? Would this be related to being in a thread environment? 这是否与线程环境有关?
Why don't you use the futures returned by ccfileimpl_exsc.submit()
? 为什么不使用ccfileimpl_exsc.submit()
返回的期货? Instead, you call ccfileimpl_exsc.shutdown()
right after submitting the jobs, killing them before they finish. 而是在提交作业后立即调用ccfileimpl_exsc.shutdown()
,并在作业完成前将其杀死。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.