如何在后台执行java traitement

xu3bshqb  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(307)

如何优化java处理(for循环、嵌套循环)并在后台执行它,以便继续进行其他处理?

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = AppTechnicalException.class)
    public void update(FicheDTO dto, String code) throws {

        Fiche fiche = this.ficheDAO.find(fiche.getNumero()) ;

        this.traitementToDO(dto, fiche);

        Tracabilite trace = createTraceF(fiche.getNumero(), code);

    }

我想要那个 this.traitementToDO(dto, fiche); 成为背景!

wfsdck30

wfsdck301#

为了在java中并行计算多个任务,可以使用线程。通过创建新线程,您可以异步运行长任务,然后在任务执行完成时发出通知。有关线程的更多信息,请参见此。

weylhg0b

weylhg0b2#

你可以试试这个-

@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = AppTechnicalException.class)
public void update(FicheDTO dto, String code) throws {

    Fiche fiche = this.ficheDAO.find(fiche.getNumero()) ;

    Thread demonThread = new Thread(() -> traitementToDO(dto, fiche));
    /*new Runnable() {
        @Override
        public void run() {
            traitementToDO(dto, fiche);
        }
    }); //lambda like anonimus class */

    daemonThread.setDaemon(true); // run as demon if needed
    daemonThread.start(); // execute method in another child thread

    Tracabilite trace = createTraceF(fiche.getNumero(), code);
}

但是想想@transactional:如果你需要 traitementToDO 按事务处理,您不能通过 this ,因为您无法访问此bean中的spring代理。所以你需要进行自动布线,然后通过 self.traitementToDO . 另外,如果您不知道如何使用线程,请小心使用自定义多线程。最后,您需要阅读如何在多线程中使用@transactional(如果您需要介绍 traitementToDO 再次交易)。

相关问题