在Java中传递runnable类构造函数中的值

时间:2012-02-13 04:31:47

标签: java multithreading

我希望有人能帮助我找出下面代码出错的地方。

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

另外作为补充问题,为什么当我尝试更新db表时没有执行更新?这与线程环境有关吗?

1 个答案:

答案 0 :(得分:0)

为什么不使用ccfileimpl_exsc.submit()返回的期货? 相反,您在提交作业后立即致电ccfileimpl_exsc.shutdown(),在完成作业之前将其杀死。