InheritableThreadLocal的使用问题

探讨了InheritableThreadLocal在多线程环境中的应用风险,特别是在使用Quartz调度器时可能出现的问题,并通过实际案例说明了如何定位此类难以察觉的线程安全问题。
单例对象,在多线程环境中使用时,为了避免线程冲突,大家都知道要通过ThreadLocal来管理该对象,为每个线程保存该对象副本,spring中管理bean、hibernated的SessionFactory管理Session都是这么做的,但是ThreadLocal有一个子类InheritableThreadLocal,使得父子线程之间能够共享实例,如
Java代码
  1. private static ThreadLocal<Context> configHolder = new InheritableThreadLocal();   
在web应用场景中能够解决一些问题,但是目前很多应用都会使用quartz,比如在晚上让系统来处理一些任务,但是当这些任务同时执行时,由于有共同的quartz的父任务,导致这些任务来操作通过InheritableThreadLocal管理的对象时,可能导致线程冲突,所以在这种场景下要慎用InheritableThreadLocal。
当时本人碰到的一个场景是,晚上系统来统计系统的访问统计信息,由于本系统是一个应用管理多个网站,每个网站的信息是通过一个Context来保存访问具体哪个网站的,当执行quartz后,有些网站的关键词显然不是这个网站的,当时觉得问题很诡异,最后通过加了大量输出时发现,在一个网站的统计还没结束时,Context里的网站已经被切换了,显然这不是自己的任务切换的,最终发现同时有另外的Quartz任务也在操作Context对象,这类问题很难定位原因,当时搞了很久才发现这个原因
使用线程池时,可能会遇到InheritableThreadLocal无法正确继承的问题。这是因为线程池在执行任务时会重用之前创建的线程,而这些线程可能已经绑定了旧的InheritableThreadLocal值,导致新任务继承错误的值。解决这个问题的办法是使用ThreadPoolExecutor而不是ThreadPool来创建线程池,并覆盖它的`ThreadFactory`方法,以创建一个新的线程并正确地继承InheritableThreadLocal值。具体来说,可以创建一个实现`ThreadFactory`接口的类,并覆盖`newThread`方法,如下所示: [^2] ```python import threading from concurrent.futures import ThreadPoolExecutor, _base class MyThreadLocal(_base.Executor): def __init__(self, thread_local): self.thread_local = thread_local def submit(self, fn, *args, **kwargs): return super().submit(self.wrapper(fn), *args, **kwargs) def wrapper(self, fn): local = self.thread_local.copy() def wrapped_fn(*args, **kwargs): with local: return fn(*args, **kwargs) return wrapped_fn def map(self, fn, *iterables, timeout=None, chunksize=1): return list(self._map_async(fn, *iterables).result(timeout=timeout)) def shutdown(self, wait=True): pass class MyThreadFactory(ThreadPoolExecutor): def __init__(self, thread_local, *args, **kwargs): self.thread_local = thread_local super().__init__(*args, **kwargs) def new_thread(self, executor, task): t = threading.Thread(target=executor._worker, args=(task,), daemon=True) t.daemon = False t.name = None t._Thread__ident = None t._target = None t._args = None t._kwargs = None t._state = threading.S t._thread_local = self.thread_local return t # 使用示例: import random import time def test_inheritable_thread_local(thread_local, pool): thread_local.value = random.randint(0, 100) pool.submit(worker, thread_local.copy()) def worker(thread_local_copy): print(thread_local_copy.value) time.sleep(1) print(thread_local_copy.value) if __name__ == '__main__': thread_local = threading.local() pool = MyThreadLocal(thread_local) factory = MyThreadFactory(thread_local, 5) pool._threads = set() pool._max_workers = 5 pool._thread_name_prefix = 'ThreadPoolExecutor-' pool._initializer = None pool._initargs = () pool._queue = queue.Queue() pool._task_counter = itertools.count() pool._shutdown = False pool._results = {} pool._work_ids = set() pool._threads_lock = threading.Lock() pool._threads_recreate_lock = threading.Lock() pool._pending_work_items_lock = threading.Lock() pool._wake_up_mutex = threading.Lock() pool._not_responsive_workers = set() pool._shutdown_thread = None pool._shutdown_lock = threading.Lock() pool._shutdown_cond = threading.Condition(pool._shutdown_lock) pool._workers = {} pool._done_with_recreate = threading.Condition() pool._threads_recreate_override = False pool._threads_recreate_count = 0 pool._threads_recreate_next_id = 0 pool._threads_recreate_idle_time = 0.0 pool._threads_recreate_rate = 0.5 pool._threads_recreate_max = 5 pool._threads_recreate_last = 0.0 pool._threads_recreate_reset = False pool._kill_workers = False pool._force_workers = set() pool._shutdown_lock = threading.Lock() pool._shutdown_cond = threading.Condition(pool._shutdown_lock) pool._stop_f = None pool._threads_recreate_condition = threading.Condition(pool._threads_recreate_lock) pool._thread_factory = factory test_inheritable_thread_local(thread_local, pool) pool.shutdown() ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值