[concurrency-interest] thread safety of java.lang.reflect.Field

Nathan and Ila Reynolds nathanila at gmail.com
Thu May 31 08:11:00 EDT 2018


I usually cache Fields in private static final fields in the class that 
I use the Fields.  I have not had any trouble with multiple threads 
using Fields concurrently.  One caution is that if two threads execute 
the statement below concurrently on the same "obj", then one of the 
increments could be lost.  In other words, one still has to be mindful 
of concurrent access to the object's fields.

s_field.setInt(obj, s_field.getInt(obj) + 1);

-Nathan

On 5/31/2018 3:31 AM, Novi via Concurrency-interest wrote:
> Hello,
>
> I wonder if instances of java.lang.reflect.Field can be shared between
> multiple threads as long as the accessibility flag is either not modified
> or modified exactly once prior to a safe publication of the field
> instances.
>
> In other words, is it legal to cache instances of j.l.r.Field between
> multiple threads?
>
> Best Regards,
> Novi
>
>
> PS: The Bean Validation reference implementation Hibernate Validator 
> seems
> to cache instances of j.l.r.Field across threads. However, I couldn't 
> find any
> clue in the Java API documentation whether such a usage is supported or
> not.
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

-- 
-Nathan



More information about the Concurrency-interest mailing list