title: Synchronize two methods in different classes (and different packages)
body: <p>I'm facing an issue regarding Java method synchronization. Let's hope I can explain it briefly:</p>
  
  <p>I have two different methods in two different classes, in two different packages. Something like:</p>
  
  <pre><code>Package1
       |_Class1
              \MethodA
  
  Package2
       |_Class2
              \MethodB
  </code></pre>
  
  <p>Ok, so now I need to synchronize these two methods, which are <strong>not threads</strong>. So far, I have two different approaches:</p>
  
  <p><strong><em>Shared semaphore</em></strong>. </p>
  
  <p>Create a shared static semaphore outside <code>Package1</code> and <code>Package2</code>, something like:</p>
  
  <pre><code> package Package3;
  
   public class SemaphoreClass {
  
      public static Semaphore;
  
  }
  </code></pre>
  
  <p>I don't really know if JVM would consider this a shared semaphore, anyway.</p>
  
  <p><strong><em>Synchronized (SharedClass.class)</em></strong>. </p>
  
  <p>Using a shared class to synchronize those two methods, something like:</p>
  
  <pre><code> public void MethodA() {
          synchronized (SharedClass.class) {
             //...
          }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public void MethodB() {
          synchronized (SharedClass.class) {
             //...
          }
  </code></pre>
  
  <p>Anyway, these are just approaches. I would like to hear what's the best way to implement what I'm trying to achieve. Having a shared semaphore for JVM would make things easier, but well, I know there must be a solution for this. Thanks in advance.</p>