如何使用线程池在grails-2.4.4应用程序中使用多个线程保存数据

时间:2017-10-28 16:01:37

标签: multithreading grails gorm threadpool executorservice

我有一个多线程程序运行一些逻辑来提供我需要保存在我的grails(2.4.4)应用程序中的数据行。我使用的是30个线程的fixedthreadpool。我的程序的骨架如下。我的期望是每个线程计算所有属性并保存在表中的一行上。但是,我看到的最终结果是有一些未保存的随机行。重复此练习后,可以看到表中未保存不同的行集。因此,总的来说,每次尝试此操作时,某些行都不会保存在表中。 GORMInstance.errors没有发现任何错误。所以,我不知道这个程序中有什么不对。

    ExecutorService exeSvc = Executors.newFixedThreadPool(30)
    for (obj in list){
     exeSvc.execute({-> finRunnable obj} as Callable)
    }

此外,这是上面代码片段调用的runnable程序。

    def finRunnable = {obj ->
     for (item in LIST-1){
         for (it in LIST-2){
          for (i in LIST-3){
            rowdata = calculateValues(item, it, i);
            GORMInstance instance = new GORMInstance();
            instance.withTransaction{
             instance.attribute1=rowdata[0];
             instance.attribute2=rowdata[1];
             ......so on..
             instance.save(flush:true)/*without flush:true, I am 
              running into HeuristicCompletion exception. So I need it 
              here. */
            }//endTransaction
          }//forloop 3
         }//forloop 2
     }//forloop 1
    }//runnable closure

0 个答案:

没有答案