在Groovy线程中记录和访问变量

时间:2014-01-21 16:54:33

标签: multithreading groovy

我的问题是关于Groovy线程及其正确用法。如果我的代码如下所示:

BatchJobDetail batchInProgress= batchInProgressService.createNewBatchJob()
SomeService someService = new SomeService();
log.info("Forking thread")
 Thread.start ({
   log.info("Starting export..")
   def exportResults = someService.getExportKeys(params)
   log.info("export done..")
   batchInProgress.isCompleted = true
 })

我的线程是否始终可以访问batchInProgress和someService变量?

此外,在这种情况下,我的线程中的日志不会打印。线程外的日志打印正常。

任何指针?

1 个答案:

答案 0 :(得分:0)

适合我 - 我必须做一些微调以创建一些对象,但下面的代码运行'standalone'

import java.util.logging.Logger

class BatchJobDetail {
    boolean isCompleted
}

class BatchInProgressService {

    BatchJobDetail createNewBatchJob() {
        println 'creating a batch job...'
        new BatchJobDetail()
    }

}

class SomeService {
    def getExportKeys(params) {
        return []
    }
}


Logger log = Logger.getLogger("")
log.info ("Log created...")

BatchInProgressService batchInProgressService = new BatchInProgressService()
BatchJobDetail batchInProgress = batchInProgressService.createNewBatchJob()
SomeService someService = new SomeService();
log.info("Forking thread")
 Thread.start ({
   log.info("Starting export..")
   def params = []
   def exportResults = someService.getExportKeys(params)
   log.info("export done..")
   batchInProgress.isCompleted = true
 } )

线程内的日志语句打印。不要以为我们曾经看过蒂姆关于你如何设置“日志”的问题。