0

下面随机调用getParentCustomers还是getAccountManagers先。当它运行时,它工作正常。但是,无论哪个被称为 second,都会将 a 传递给null它。这些方法都不会以任何方式改变传入的值。我猜想有一些关于上下文的东西,这些是从原始指针response.salesChannels在任务之间丢失的情况中调用的。

Map response = [
    salesChannels:   null,
    accountManagers: null,
    parentCustomers: null,
    isrs:            null,
    operatingUnits:  null,
    businessUnits:   null
]

def t1 = task {
    response.salesChannels = salesChannelApiService.get(salesChannel)

    def t1a = task {
        response.parentCustomers = salesChannelTransformService.getParentCustomers(response.salesChannels)
    }
    def t1b = task {
        response.accountManagers = salesChannelTransformService.getAccountManagers(response.salesChannels)
    }

    waitAll([t1a, t1b])
}

def t2 = task {
    //... other stuff
}

def t3 = task {
    //... other stuff
}

waitAll([t1, t2, t3])

return response

我什至尝试修改内部结构以利用onComplete

...
onComplete([task {
    return salesChannelApiService.get(salesChannel)
}], { salesChannels ->
    response.salesChannels = salesChannels

    def t1a = task {
        response.parentCustomers = salesChannelTransformService.getParentCustomers(salesChannels)
    }
    def t1b = task {
        response.accountManagers = salesChannelTransformService.getAccountManagers(salesChannels)
    }

    waitAll([t1a, t1b])
})
...

但是,我仍然得到相同的结果。

注意:这也是随机的。有时它工作正常 - 将相同的列表传递给两种方法。但是,当它破裂时,总是先触发第二个。

对此有什么想法吗?

4

2 回答 2

0

目前,以下解决方案正在运行。但是,我不知道为什么上述代码块都没有。

有什么想法吗?

def t1 = createPromise()

task {
    response.salesChannels = salesChannelApiService.get(salesChannel)

    def t1a = createPromise()
    def t1b = createPromise()

    task {
        response.parentCustomers = salesChannelTransformService.getParentCustomers(response.salesChannels)
        t1a.accept()
    }
    task {
        response.accountManagers = salesChannelTransformService.getAccountManagers(response.salesChannels)
        t1b.accept()
    }

    onComplete([t1a, t1b], { a, b -> t1.accept() })
}
于 2020-04-15T18:12:37.640 回答
0

所以我认为线程在极少数情况下会起作用,所以我删除了 getParentCustomers 和 getAccountManagers 之间的单独线程,因为它们无论如何都不会进行外部调用。

我认为跨线程同时使用 response.salesChannels 会弄乱 response.salesChannels 的内存分配 - 它每隔几百次调用就会随机抛出一个空指针异常。

这似乎更可靠。

def t1 = createPromise()

task {
    response.salesChannels = salesChannelApiService.get(salesChannel)

    def t1a = createPromise()

    task {
        response.parentCustomers = salesChannelTransformService.getParentCustomers(response.salesChannels)
        response.accountManagers = salesChannelTransformService.getAccountManagers(response.salesChannels)
        t1a.accept()
    }

    onComplete([t1a], { a -> t1.accept() })
}
于 2020-04-16T04:16:45.667 回答