[concurrency-interest] hashing the reflect.Method object in ConcurrentHashMap

Stanimir Simeonoff stanimir at riflexo.com
Thu May 16 14:20:16 EDT 2013


On Thu, May 16, 2013 at 6:14 PM, Andy Nuss <andrew_nuss at yahoo.com> wrote:

> But calling .invoke is quite fast, and getting the Method is very slow,
> and I want to invoke many times for all the threads that use this class to
> invoke the Method, each one passing its own object to invoke.  Does this
> mean that I have to synchronize on the Method instance during invoke?
>
> No. No synchronization is needed, but you should publish the method via
volatile field or AtomicReference/CHM/etc.
The underlying implementation that  carries invoke() is actually shared but
that's beyond the point.

Stanimir

>
>   ------------------------------
>  *From:* Stanimir Simeonoff <stanimir at riflexo.com>
> *To:* Andy Nuss <andrew_nuss at yahoo.com>
> *Cc:* "concurrency-interest at cs.oswego.edu" <
> concurrency-interest at cs.oswego.edu>
> *Sent:* Thursday, May 16, 2013 7:16 AM
> *Subject:* Re: [concurrency-interest] hashing the reflect.Method object
> in ConcurrentHashMap
>
> Each call to Class.getMethod/getDeclaredMethod has to return a new
> instance as the class is modifiable (setAccessible). As the safety goes:
> looking at the impl. it is unsafe.
> More also searching through the available methods is O(n), that is each
> call searches through all the declared methods.
>
> Stanimir
>
>
> On Thu, May 16, 2013 at 4:18 PM, Andy Nuss <andrew_nuss at yahoo.com> wrote:
>
> Simple questions, relating to hashing a Method object returned by
> Class.getDeclaredMethod, where the key is the Class:
>
> Is there only one instance of Method in the vm for a given actual object
> method, or does a new Method object get created for each call to
> Class.getDeclaredMethod?  Is there any need when a thread obtains a Method
> object to publish it safely from one thread to the other or can it be
> shared thru back-door non-volatile locations in memory?
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130516/1596d58e/attachment.html>


More information about the Concurrency-interest mailing list