java.lang.reflect.Method线程安全吗? [英] Is java.lang.reflect.Method thread safe?

查看:141
本文介绍了java.lang.reflect.Method线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.lang.reflect.Method 线程安全?

我的程序的性能分析结果显示 Class.getMethod()多次调用需要相当多的计算时间,比我预期的多一点。
我可以调用一次,并将生成的方法存储在易于访问的地方。
但是,多个Web工作线程将同时使用存储的 Method 对象。

Profiling result of my program showed that Class.getMethod() took considerable computing time when called many times, a little more than I expected. I can call this once and store the resulting method somewhere easily accessible. But then, multiple web worker threads will use the stored Method object concurrently.

这是否安全?

推荐答案

方法可以安全地使用多个线程,前提是您在多个线程可用后不更改方法的状态threads.eg您可以在两个线程中调用setAccessible(true)和setAccessible(false),结果将不是线程安全的。然而,这没有什么用处。

The Method is safe to use accross multiple threads provided you don't change the Method's state after making it available to multiple threads.e.g. You can call setAccessible(true) and setAccessible(false) in two threads and the result would be not thread safe. However this has no really good use.

简而言之,Method.setAccessible()不是技术上的线程安全,但你应该能够以线程安全的方式使用它。

In short, Method.setAccessible() is not techincally thread safe, but you should be able to use it in a thread safe way.

这篇关于java.lang.reflect.Method线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆