How to use MDC with ForkJoinPool?

前端 未结 3 1559
攒了一身酷
攒了一身酷 2020-12-31 22:03

Following up on How to use MDC with thread pools? how can one use MDC with a ForkJoinPool? Specifically, I how can one wrap a ForkJoinTask so MDC v

3条回答
  •  借酒劲吻你
    2020-12-31 22:37

    I am unfamiliar with ForkJoinPool but you can pass the MDC key/values of interest to the ForkJoinTask instances that you instantiate before submitting them to the ForkJoinPool.

    Given that as of logback version 1.1.5, MDC values are not inherited by child threads, there are not too many options. They are

    1. pass the relevant MDC key/values to ForkJoinTask instances as you instantiate them
    2. extend ForkJoinPool so that MDC key/values are passed to the newly created threads
    3. create your own ThreadFactory which sets MDC key/values to newly created threads

    Please note that I have not actually implemented either options 2. or 3.

提交回复
热议问题