title: Java, Make a method that can only be called once at a time
body: <p>I have an application that uses a read write lock to prevent other methods from running when the so called "reconnect" method is called. Now, I want this "reconnect" method to only be called once at a time. For example, when Thread1 calls "reconnect" and while "reconnect" is executing, Thread2 calls it, it either immediately returns or waits until the call of Thread1 is finished and then returns. (Resulting in only one execution). As you can probably imagine I have an application that interacts with some API and when my session times out, it needs to reconnect but I don't want every thread creating a new connection since that would be completely unnecessary. I hope I provided enough information.</p>