希望多线程代码作为单个事务工作

时间:2015-10-27 05:48:45

标签: java multithreading transactions synchronization

我有一个for循环,并且内部存在一个具有多线程代码的方法(updateBalancePnlForAllPermissions)。我的问题是我无法从updateBalancePnlForAllPermissions()中删除多线程概念。但在这种特殊情况下,我希望updateBalancePnlForAllPermissions()保留其多线程特性。 我希望即使for循环中的单个条目在此方法中失败,也应该停止整个过程,并且应该还原在db中创建的任何条目。 目前,如果一个条目发生故障,则第二个条目的执行仍然继续。我可以说,我将forloop作为单个事务工作,只要发生故障,整个事务就会回滚。有人可以帮忙吗?我是多线程的新手,在这里感到迷茫。

for (AppDate cobDate : recordsForCob.keySet()) {
    synchronized (BalanceServiceImpl.class) {
        PnlFile cobSpecificPnlFile
            = new PnlFile(pnlFile.getHeader(), pnlFile.getFooter(), Lists.newArrayList(recordsForCob.get(cobDate)));
        List<PnlBook> pnlBooks=getPnlBooks(cobSpecificPnlFile);
        try{
            updateBalancePnlForAllPermissions
                (cobDate, pnlFile.getHeader().getPnlCentre(), JEDI, pnlBooks, false, file);
        } catch(AlreadyRunningException exc){
            logger.error(exc);
        }catch(Exception e){
            logger.error(e);
            updatePnlUploadStatus(e.getMessage(), dirLocation,file);
            break;
       }    

0 个答案:

没有答案