-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
如何子线程向父线程传递值? #125
Comments
你的需求场景是和要解决的问题是什么,可以先具体些 说明一下。 😄 @zongchangbo 简单地说
这即是 支持了 子线程向父线程 传递,注意线程安全。 展开来说
关于『
讨论和使用注意参见
|
嗯 谢谢啊,TTL缺省向子线程传递的是引用, 而我之前用的是String 这种弱引用类型 ,所以失败了 |
COOL 解决了就好 ❤️ 能说明一下你的需求场景/使用场景吗? @zongchangbo ❤️ 方便 的话,请记到 Issue 【用户反馈收集】说一下您的使用场景、使用项目、公司,感谢! 中 :) PS 关于
|
下面的这个demo是可以的: public class CustomThreadLocal {
static TransmittableThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
CustomThreadLocal.threadLocal.set("test: " + new Random(10).nextInt());
new Service().call();
}
}).start();
}
}
class Service {
public void call() {
//System.out.println("Service:" + Thread.currentThread().getName());
System.out.println("Service:" + CustomThreadLocal.threadLocal.get());
new Dao().call();
}
}
class Dao {
public void call() {
System.out.println("==========================");
// System.out.println("Dao:" + Thread.currentThread().getName());
System.out.println("Dao:" + CustomThreadLocal.threadLocal.get());
}
} 但是我实际工作中的不行 @Slf4j
public class PrometheusMetricsInterceptor extends HandlerInterceptorAdapter {
static final Counter requestCounter = Counter.build()
.name("cuishoufen_http_requests_total").labelNames("path", "method", "code")
.help("Total requests.").register();
public static final TransmittableThreadLocal<String> localStatus = new TransmittableThreadLocal<String>();
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
String requestURI = request.getRequestURI();
String method = request.getMethod();
int status = response.getStatus();
requestCounter.labels(requestURI, method, localStatus.get() == null ? String.valueOf(status): localStatus.get()).inc();
localStatus.remove();
super.afterCompletion(request, response, handler, ex);
}
/////////////////////////////////////////////////////////
@HystrixCommand(
fallbackMethod = "getCuishouTag_fallback",
groupKey = "getCuishouTag",
commandKey = "getCuishouTag",
threadPoolKey = "getCuishouTag",
commandProperties = {
//设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行回退逻辑
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"),
@HystrixProperty(name = "fallback.isolation.semaphore.maxConcurrentRequests", value = "200"),
//设置在一个滚动窗口中,打开断路器的最少请求数
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "4"),
//设置在回路被打开,拒绝请求到再次尝试请求并决定回路是否继续打开的时间
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "60000"),
//设置统计的滚动窗口的时间段大小。该属性是线程池保持指标时间长短
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "180000")
},
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
@HystrixProperty(name = "maxQueueSize", value = "200"),
//设置队列拒绝的阈值——一个人为设置的拒绝访问的最大队列值,即使maxQueueSize还没有达到
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "50"),
//设置统计的滚动窗口的时间段大小。该属性是线程池保持指标时间长短
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "180000")
}
)
@Override
public HashMap<String, String> getCuishouTag(String tel) {
HashMap<String,String> hm = new HashMap<String,String>();
try {
String param = buildParam(tel);
String response = getResponse(URL_2, param);
if (StringUtils.isNotBlank(response)) {
hm = parseResponse(response);
}
}catch (Exception e){
PrometheusMetricsInterceptor.localStatus.set("9999")
log.error("getCuishouTag process error, tel={}", tel, e);
return hm;
}
return hm;
}
} |
然后我的解决方案就是把String 换成一个对象,请问为何会发生这样的事情? |
因为 涉及 这些 框架 我没有具体用过、不熟悉了解,所以 你要自己去分析一下了。 😄 |
解决了吗? 我现在也遇到了同样的问题。我有一个需求是收集所有子线程的执行日志然后到父线程汇总 。 |
微信说吧,809116583
…------------------ 原始邮件 ------------------
发件人: "alibaba/transmittable-thread-local" ***@***.***>;
发送时间: 2022年7月19日(星期二) 晚上6:28
***@***.***>;
***@***.******@***.***>;
主题: Re: [alibaba/transmittable-thread-local] 如何子线程向父线程传递值? (#125)
目前发现的都是主线程到子线程的 值传递; 那从子线程到主线程的 却不能实现,这个有办法解决吗?
解决了吗? 我现在也遇到了同样的问题。我有一个需求是收集所有子线程的执行日志然后到父线程汇总 。
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
目前发现的都是主线程到子线程的 值传递;
那从子线程到主线程的 却不能实现,这个有办法解决吗?
The text was updated successfully, but these errors were encountered: