Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is java.lang.reflect.Method thread safe?

Tags:

java

Is java.lang.reflect.Method thread safe?

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.

Is this safe?

like image 593
bungrudi Avatar asked Mar 18 '11 07:03

bungrudi


1 Answers

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.

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

like image 91
Peter Lawrey Avatar answered Sep 18 '22 21:09

Peter Lawrey