繁体   English   中英

在Java中的可运行类构造函数中传递值

[英]Passing values in runnable class constructor in Java

我希望有人可以帮助我确定下面的代码在哪里出了问题。

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();
    }
}

上面的代码检索文件进行处理,然后将其分配给可运行类(如下),然后将其提交给executorService类。

现在,我不明白为什么将构造函数的传递值(以下)设置为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);
        }
    }
}

另外,作为补充问题,为什么在我尝试更新数据库表时未执行任何更新? 这是否与线程环境有关?

为什么不使用ccfileimpl_exsc.submit()返回的期货? 而是在提交作业后立即调用ccfileimpl_exsc.shutdown() ,并在作业完成前将其杀死。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM