如何在后台执行java traitement

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

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

  1. @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = AppTechnicalException.class)
  2. public void update(FicheDTO dto, String code) throws {
  3. Fiche fiche = this.ficheDAO.find(fiche.getNumero()) ;
  4. this.traitementToDO(dto, fiche);
  5. Tracabilite trace = createTraceF(fiche.getNumero(), code);
  6. }

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

wfsdck30

wfsdck301#

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

weylhg0b

weylhg0b2#

你可以试试这个-

  1. @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false, rollbackFor = AppTechnicalException.class)
  2. public void update(FicheDTO dto, String code) throws {
  3. Fiche fiche = this.ficheDAO.find(fiche.getNumero()) ;
  4. Thread demonThread = new Thread(() -> traitementToDO(dto, fiche));
  5. /*new Runnable() {
  6. @Override
  7. public void run() {
  8. traitementToDO(dto, fiche);
  9. }
  10. }); //lambda like anonimus class */
  11. daemonThread.setDaemon(true); // run as demon if needed
  12. daemonThread.start(); // execute method in another child thread
  13. Tracabilite trace = createTraceF(fiche.getNumero(), code);
  14. }

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

展开查看全部

相关问题