gpt4 book ai didi

multithreading - 常规/ chalice : pass parameter to thread

转载 作者:行者123 更新时间:2023-12-04 06:42:43 24 4
gpt4 key购买 nike

在 grails Controller 中启动了一个新线程(任务/ promise )。但是当我尝试在线程中获取它时,用户为空。

if(springSecurityService.currentUser){
task {
// do something with the current user
// springSecurityService.currentUser is null here!!!!!!
}.get()
}

如何将用户作为参数传递给任务?

最佳答案

问题不在于“用户离开 Controller 后启动线程”,而是 springSecurityService.currentUser 是线程本地状态,当从不同的线程调用。但是,如果您将此线程的 springSecurityService.currentUser 的值 保存在局部变量中,您应该能够在闭包内访问它:

if(springSecurityService.currentUser){
def theUser = springSecurityService.currentUser
task {
// do something with theUser
}.get()
}

关于multithreading - 常规/ chalice : pass parameter to thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24014247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com