When and how should I use a ThreadLocal variable?

前端 未结 25 1890
再見小時候
再見小時候 2020-11-22 12:35

When should I use a ThreadLocal variable?

How is it used?

25条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 12:54

    The documentation says it very well: "each thread that accesses [a thread-local variable] (via its get or set method) has its own, independently initialized copy of the variable".

    You use one when each thread must have its own copy of something. By default, data is shared between threads.

提交回复
热议问题